PeeNpee PeePeen PeeNpee PeePeen - 4 months ago 14
Java Question

android camera intent returns no data

I have a problem with obtaining data from on the 'onActivityResult' method after taking a picture from the camera intent.

Here is my method which is calling camera Intent:

private void __initCamera() {

String path = Environment.getExternalStorageDirectory() + "/vault/images/";

File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
dir = new File(path);
}
String[] content = dir.list();
String fileName = (content.length + 1) + ".jpg";
File file = new File(path, fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException ex) {
Log.e("my_app", ex.getMessage());
}
}

Uri uri = Uri.fromFile(file);
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(i, 8765);
}


Here is my onActivityResult method:

returnCode:
-1(should be: 1, right?),


exception:
java.io.FileNotFoundException: No content provider: #Intent;action=inline-data;end


data.toURI() :
#Intent;action=inline-data;end


@Override
protected void onActivityResult(int requestCode, int returnCode, Intent data) {

try {

Bitmap bitmap = Media.getBitmap(getContentResolver(), Uri.parse(data.toURI()));
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
bitmap.recycle();
out.close();
} catch (Exception ex) {
Log.e("TROLL", ex.toString());
}
}

Answer

This:

Bitmap bitmap = Media.getBitmap(getContentResolver(), Uri.parse(data.toURI()));

Should be this:

Bitmap bitmap = (Bitmap) data.getExtras().get("data"); 

At least, that's how I normally retrieve images. Also, I would recommend checking for the correct request code instead of using an all encompassing Exception there.

Comments