Saturday, September 24, 2016

App is crashing after capturing picture using intents

Leave a Comment

My app is crashing after capturing 5 to 6 photos using intents.log cat shows nothing. am unable to find the reason why it is crashing. please help me out.

    private void capturePhoto() {          File root = new File(Environment.getExternalStorageDirectory(), "Feedback");         if (!root.exists()) {             root.mkdirs();         }         File file = new File(root, Constants.PROFILE_IMAGE_NAME + ".jpeg");         Uri outputFileUri = Uri.fromFile(file);           Intent photoPickerIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);         photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);         photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());         photoPickerIntent.putExtra("return-data", true);         photoPickerIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);         startActivityForResult(photoPickerIntent, requestCode);       }       @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         super.onActivityResult(requestCode, resultCode, data);         if (this.requestCode == requestCode && resultCode == RESULT_OK) {              File root = new File(Environment.getExternalStorageDirectory(), "Feedback");             if (!root.exists()) {                 root.mkdirs();             }             File file = new File(root, Constants.PROFILE_IMAGE_NAME+".jpeg");             checkFlowIdisPresent(file);              displayPic();           }     }   private void displayPic() {          String filePath = Environment.getExternalStorageDirectory()                 .getAbsolutePath() + File.separator + "/Feedback/" + Constants.PROFILE_IMAGE_NAME + ".jpeg";         //  Bitmap bmp = BitmapFactory.decodeFile(filePath);         //Bitmap scaled = Bitmap.createScaledBitmap(bmp, 300, 300, true);           File imgFile = new File(filePath);         Bitmap bmp = decodeFile(imgFile);          if (imgFile.exists()) {              dispProfilePic.setImageBitmap(bmp);         } else {             dispProfilePic.setBackgroundResource(R.drawable.user_image);          }     }   private Bitmap decodeFile(File f) {         try {             // Decode image size             BitmapFactory.Options o = new BitmapFactory.Options();             o.inJustDecodeBounds = true;             BitmapFactory.decodeStream(new FileInputStream(f), null, o);              // The new size we want to scale to             final int REQUIRED_SIZE = 70;              // Find the correct scale value. It should be the power of 2.             int scale = 1;             while (o.outWidth / scale / 2 >= REQUIRED_SIZE &&                     o.outHeight / scale / 2 >= REQUIRED_SIZE) {                 scale *= 2;             }              // Decode with inSampleSize             BitmapFactory.Options o2 = new BitmapFactory.Options();             o2.inSampleSize = scale;             return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);         } catch (FileNotFoundException e) {         }         return null;     } 

And above code is for capturing photo and displaying captured picture in ImageView. And am using MI tab.

Edit actually app is not crashing...it becomes white screen and if i press any button then it is crashing and onActivityResult is not executed when it become white screen

New Edit Am able to replicate this. I clicked on Android Monitor in that i clicked Monitor. Then it shows memory utilization of the app when i interacting with app. now in left side bar i clicked terminate application icon. Now the interesting thing is it destroys current activity and moves to previous activity. That previous activity become white screen.

Please help me out guys.

9 Answers

Answers 1

Try this code. I use it in some of my apps :

Launch intent method:

private void launchCamera() {         Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);         startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);     } 

Capturing result:

@Override     public void onActivityResult(int requestCode, int resultCode, Intent data) {         super.onActivityResult(requestCode, resultCode, data);         try {             if (requestCode == CAMERA_PIC_REQUEST) {                 if (data != null) {                     Bundle extras = data.getExtras();                     if (extras != null) {                         Bitmap thumbnail = (Bitmap) extras.get("data");                         if (thumbnail != null)                             displayPic(thumbnail);                     }                 }             }             } catch (Exception e) {             e.printStackTrace();             }     } 

Answers 2

Well your code fine....

I think you save the image or overwrite image on same path with same name so there is problem with memory. So I recommended you change the name with System.currentTimeMillis() or any random name Instead of Constants.PROFILE_IMAGE_NAME.

And Also check the permission

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Also check this permission with run time also...for run time follow this

private static final int REQUEST_RUNTIME_PERMISSION = 123;       if (CheckPermission(demo.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {           capturePhoto();      } else {         // you do not have permission go request runtime permissions         RequestPermission(demo.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);     }    public void RequestPermission(Activity thisActivity, String Permission, int Code) {         if (ContextCompat.checkSelfPermission(thisActivity,                 Permission)                 != PackageManager.PERMISSION_GRANTED) {             if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,                     Permission)) {                 capturePhoto();              } else {                 ActivityCompat.requestPermissions(thisActivity,                         new String[]{Permission},                         Code);             }         }     }      public boolean CheckPermission(Activity context, String Permission) {         if (ContextCompat.checkSelfPermission(context,                 Permission) == PackageManager.PERMISSION_GRANTED) {             return true;         } else {             return false;         }     } 

Answers 3

This happens possibly because the Calling Activity gets killed and then restarted by OS as IMAGE CAPTURE intent deals with huge amount of memory for processing the BITMAP captured via CAMERA.

Solution: Save the file path of the Image and use it when onActivityResult is called. You can use onSavedInstanceState and onRestoreInstanceState methods to save and retrieve the IMAGE_PATH and other fields of the activity.

You can refer to this link for how to use onSavedInstanceState and onRestoreInstanceState

Answers 4

Try to use below code. It works fine for me.

 private static final int REQUEST_CAMERA = 1;   @Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);      if (resultCode == RESULT_OK)     {         if (requestCode == REQUEST_CAMERA)         {             Bitmap thumbnail = (Bitmap) data.getExtras().get("data");             ByteArrayOutputStream bytes = new ByteArrayOutputStream();             thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);              File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");              FileOutputStream fos;              try             {                 destination.createNewFile();                 fos = new FileOutputStream(destination);                 fos.write(bytes.toByteArray());                 fos.close();             }             catch (FileNotFoundException fnfe)             {                 fnfe.printStackTrace();             }             catch (IOException ioe)             {                 ioe.printStackTrace();             }             ivSetImage.setImageBitmap(thumbnail);          }    } } 

In the given code snippet, I have compressed the captured image, due to which app crashing problem is resolved.

In your case, the captured image quality might be high due to which your app is crashing while setting up an image on ImageView.

Just try compressing an image. It will work!

Don't forget to add permission in manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Answers 5

Try doing it in an Async task because the issue u facing is due to the hug processing done in UI thread

refer here for more help on Async task implementation

Answers 6

If nothing is displayed on the log cat it is very difficult to speculate anything but please check whether the problem is when using the emulator and not on a real device. You can also check if you can recreate the problem by making the emulator capacity smaller (Ram and internal memory). If that is the case, then increase the memory or ram of your emulator and it should work fine. You then need to work on optimizing you image processing task for lower spec devices.

Hope this helps.

Answers 7

This may be memory problem you are taking photos and storing them in bitmap Check your android Monitor for Memory Detection of APp Just make this method static

private static Bitmap decodeFile(File f) {         try {             // Decode image size             BitmapFactory.Options o = new BitmapFactory.Options();             o.inJustDecodeBounds = true;             BitmapFactory.decodeStream(new FileInputStream(f), null, o);              // The new size we want to scale to             final int REQUIRED_SIZE = 70;              // Find the correct scale value. It should be the power of 2.             int scale = 1;             while (o.outWidth / scale / 2 >= REQUIRED_SIZE &&                     o.outHeight / scale / 2 >= REQUIRED_SIZE) {                 scale *= 2;             }              // Decode with inSampleSize             BitmapFactory.Options o2 = new BitmapFactory.Options();             o2.inSampleSize = scale;             return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);         } catch (FileNotFoundException e) {         }         return null;     } 

Save files with different names like saving with timestamp as name

Answers 8

check your Manifast.xml file permission External Storage

and Camera permission.


if your App run on Marshenter code heremallow check run time permission

Answers 9

Try to use below code:

private void launchCamera() {     Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);     startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);     try {         if (requestCode == CAMERA_PIC_REQUEST) {             if (data != null) {                 Bundle extras = data.getExtras();                 if (extras != null) {                     Bitmap thumbnail = (Bitmap) extras.get("data");                     if (thumbnail != null)                         displayPic(thumbnail);                 }             }         }         } catch (Exception e) {         e.printStackTrace();         } } 

http://home2home.vn

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment