Android : How to save image from default Camera

on Thursday, July 3, 2014


I am having trouble trying to let the user take an image, and save that to a file.I would like to save this to Internal Storage. I'm currently getting a "File /data/data/package com.yo.cameraTest/files/filename.jpg contains a path separator." What am I doing wrong?


Code:



package com.yo.cameraTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

public class CameraCaptureActivity extends FragmentActivity {

protected boolean _taken = true;
File sdImageMainDirectory;
static String m_fileStrFrom;
FileOutputStream m_fos;

protected static final String PHOTO_TAKEN = "photo_taken";


@Override
public void onCreate(Bundle savedInstanceState) {

try {

super.onCreate(savedInstanceState);

Bundle extras = getIntent().getExtras();
String fileName=null;
String pathName=null;
if (extras != null)
{
fileName = extras.getString("fileName");
pathName = extras.getString("pathName");
}


m_fileStrFrom = this.getFilesDir() + "/" + fileName + ".jpg";

m_fos = this.openFileOutput(m_fileStrFrom, Context.MODE_PRIVATE);

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(m_fileStrFrom)));

startActivityForResult(intent, 0);
}

catch(Exception e) {
Log.e("CameraCaptureActivity::onCreate Exception caught: ",e.getMessage());
finish();

}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case 0:
finish();
break;

case -1:

try {
Log.e("DudeCameraActivity6: ","");
StoreImage(this,Uri.parse(m_fileStrFrom), sdImageMainDirectory);

} catch (Exception e) {
e.printStackTrace();
}

finish();

}

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(CameraCaptureActivity.PHOTO_TAKEN)) {
_taken = true;
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(CameraCaptureActivity.PHOTO_TAKEN, _taken);
}

public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {
Bitmap bm = null;
try {

bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);
final FileOutputStream out = mContext.openFileOutput( m_fileStrFrom, Context.MODE_PRIVATE) ;
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
bm.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

}

}

0 comments:

Post a Comment