Reputation: 43
I'm trying to take pictures with a camera Intent, but the pictures written to the storage are empty (size: 0 Bytes). Can someone tell me what's wrong with my code?
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnImageCapture:
preinsertedUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, OPEN_CAMERA);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case OPEN_CAMERA:
if (resultCode != 0 && data != null) {
Uri imageUri = null;
if (data != null){
imageUri = data.getData();
}
if(imageUri == null && preinsertedUri != null){
imageUri = preinsertedUri;
}
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
filePath = cursor.getString(columnIndex);
break;
}
}
}
Upvotes: 3
Views: 2648
Reputation: 3189
Try this code I posted: https://stackoverflow.com/a/14965840/1291514 . It also has a method for saving a picture to another location in onActivityResult.
Upvotes: 0
Reputation: 1529
try this code its working for me
case OPEN_CAMERA:
if (resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
saveBitmap(photo, pathToStrore);
}
break;
public void saveBitmap(Bitmap photo, String path) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(path);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// write the bytes in file
}
Upvotes: 3