I am trying to set the image of my imageButtons when a user is selecting a photo from the gallery or is taking a photo using the camera. The problem is that my imageButtons are just changed to a blank image, but I am getting the file directory. What am I doing wrong? I have created my ImageIntent and onActivityResult from this answer camera & gallery. But here is my onActivityResult method:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action
.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
} else {
selectedImageUri = data == null ? null : data.getData();
}
ImageButton pic1 = (ImageButton) findViewById(R.id.ibPic1);
Toast.makeText(this, "Image saved to:\n" + selectedImageUri,
Toast.LENGTH_LONG).show();
pic1.setImageURI(selectedImageUri);
}
}
}
So i know from the Toast that I am getting the Uri's. I have tried this answer, and various other solutions which involves some sort of Bitmap, but these are always resulting in app crash and out of memory exceptions.
Edit
OnClick method to launch the image intent:
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ibPic1:
openImageIntent();
break;
}
}
Image intent method
private void openImageIntent() {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Klea" + File.separator);
root.mkdirs();
final String fname = Sell.getUniqueImageFilename();
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
"Vælg kilde");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE);
}
Get unique file name method:
private static String getUniqueImageFilename() {
// TODO Auto-generated method stub
String fileName = "img_" + System.currentTimeMillis() + ".jpg";
return fileName;
}