0

I need to take photos in my application, this is the core codes:

tigger:

mPhotoToBeUpload = new File(mImageBasePath, System.currentTimeMillis() + ".jpg");
try {
    mPhotoToBeUpload.mkdirs();
    mPhotoToBeUpload.createNewFile();
} catch (IOException e) {
    Log.e("xx",e.getMessage());
}

Uri outputFileUri = Uri.fromFile(mPhotoToBeUpload);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

startActivityForResult(cameraIntent, Result_Code_Camera);

Result handler:

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    if (resultCode == RESULT_OK) {
        Bitmap photoTaken = BitmapFactory.decodeFile(mPhotoToBeUpload.getAbsolutePath());
        mImageView.setImageBitmap(photoTaken);
    } else
}

However I found that these codes work in some devices but not in some others.

For example, they does not work in Nexus 4 and Nexus5.

While I saythey does not work,I mean that after I take a picture, and hit the OK button, I cannot get back to the previous activity:

enter image description here

It have no effect when I hit the OK(the icon inside the red circle).

What is the problem?


Upadte:

private final String mImageBasePath = Environment.getExternalStorageDirectory() + File.separator + IConstant.Application_Folder + File.separator + "photos";
mPhotoToBeUpload = new File(mImageBasePath, System.currentTimeMillis() + ".jpg");

Result: mPhotoToBeupLoad=/storage/emulated/0/myapp/photos/1395287647386.jpg

hguser
  • 35,079
  • 54
  • 159
  • 293

2 Answers2

0

Show what is your path for mImageBasePath, because this problem mostly happen when the file path is invalid.

  • you can try copying and pasting this to your codes and see if it works. mPhotoToBeUpload = new File(getExternalStorageDirectory() + File.separator + "myapp" + File.separator + "photos" + File.separator " + "1395287647386.png"); Uri outputFileUri = Uri.fromFile(mPhotoToBeUpload); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(cameraIntent, Result_Code_Camera); – TinyTiny Mar 20 '14 at 04:09
0

Solved by referring this question:Android ACTION_IMAGE_CAPTURE Intent

Instead of

Uri outputFileUri = Uri.fromFile(mPhotoToBeUpload);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

startActivityForResult(cameraIntent, Result_Code_Camera);

Use:

URI mPhotoToBeUploadURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoToBeUploadURI);
Community
  • 1
  • 1
hguser
  • 35,079
  • 54
  • 159
  • 293