I have gone through most of the related question regarding the activityresult returning null using camera Intent. Here is my code below:
String filepath;
File file;
Bitmap bitmap;
int previewcount=0;
@OnClick(R.id.lnrcamera)
public void opencamera()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(this.getPackageManager()) != null)
{
try
{
file = createImageFile();
}
catch (IOException ex)
{
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (file != null)
{
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, FILE_CAMERA);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
// Save a file: path for use with ACTION_VIEW intents
filepath = "file:" + image.getAbsolutePath();
return image;
}
On ActivityResult
else if (requestCode==FILE_CAMERA && resultCode == RESULT_OK)
{
try
{
Log.d("sdfasdfasdf", "onActivityResult: "+filepath.isEmpty());
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(filepath));
int newHeight = (bitmap.getHeight() * 500)/bitmap.getWidth();
bitmap = Bitmap.createScaledBitmap(bitmap, 500, newHeight, false);
list_bitmap.add(bitmap);
lnrpreview.setVisibility(View.VISIBLE);
Adapter_Image adapter_expenseCategory=new Adapter_Image(this, 0, list_bitmap, new RecyclerCallback() {
@Override
public void onPreviewDelete(List<Bitmap> list_bitmap)
{
Log.d("hereornot", "onPreviewDelete: "+list_bitmap.size());
previewcount--;
if (list_bitmap.size()==0)
{
lnrpreview.setVisibility(View.GONE);
imgpreviewcount.setImageResource(R.drawable.flagoff);
}
btxtpreviewcount.setBadgeCount(previewcount);
}
});
recycler.setAdapter(adapter_expenseCategory);
previewcount++;
btxtpreviewcount.setBadgeCount(previewcount);
imgpreviewcount.setImageResource(R.drawable.flagon);
}
catch (OutOfMemoryError e)
{
Toast.makeText(Activity_NewExpense.this, "Your device is out of Memory. Please free up some space.", Toast.LENGTH_SHORT).show();
}
catch (IndexOutOfBoundsException e)
{
Log.d("hereornot", "onPreviewDelete: "+e.getMessage());
}
catch (FileNotFoundException e)
{
Log.d("hereornot", "onPreviewDelete: "+e.getMessage());
}
catch (IOException e)
{
e.printStackTrace();
}
}
Now the question is whenever I tried to open camera and get some photos the pictures are taken and it is in the memory too but the activity wont return me the file path. The testing device is running low on memory.However, for 1 or two clicks it works fine using camera, But more than two clicks it crashes and says this error :
Unable to resume activity java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=null} to activity java.lang.NullPointerException at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2996) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3025) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3974) at android.app.ActivityThread.access$1000(ActivityThread.java:166) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
Since the image might be big, I resized it and set the largeheap=true
but unlucky me it didn't work. Could anyone guide me where I did wrong?
Most likely, your process is being terminated when it is in the background. You need to hold onto your file location in the saved instance state Bundle
, so you can get it back if needed.
This sample project (from this book) illustrates how to use the saved instance state Bundle
to hold onto the file location. It also demonstrates how to use FileProvider
, which you will need if you want your app to work on Android 7.0+ devices, as Uri.fromFile()
will not work.