Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App is crashing after capturing picture using intents

Tags:

android

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.

like image 500
venkatesh gowda Avatar asked Sep 09 '16 11:09

venkatesh gowda


People also ask

How do you fix an app that keeps crashing?

To fix Android apps that keep crashing: To do this, go to Settings and open Apps. Under Your apps, you'll see a list of the apps currently installed on your device. From the list, tap the app that keeps crashing and tap Force stop in the bottom right corner. Then try opening the app again.

Why is my app suddenly crashing?

This usually occurs when your Wi-Fi or cellular data is slow or unstable, causing apps to malfunction. Another reason for Android apps crashing can be a lack of storage space in your device. This can occur when you overload your device's internal memory with heavy apps.

What happens if an app keeps closing?

In some instances, an app may force close, crash, frequently freeze or stop responding, or generally not work as the app was designed. This can be caused by many factors, but most app issues can be fixed by updating the software or clearing the app data.


2 Answers

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();
            }
    }
like image 106
ramin eftekhari Avatar answered Sep 21 '22 08:09

ramin eftekhari


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;
        }
    }
like image 36
Arjun saini Avatar answered Sep 18 '22 08:09

Arjun saini