1

I developed an application where you can take some photos, the main problem is that if I go in the background during the Intent Camera and after a while I put the application back in the foreground (which already has the active Camera) and click the photo is brought back to the application with the Toast "Error while capturing Image" and ALL the variables are totally lost, therefore clicking on any button of the application this brings me back to the login. The variables are also reset for all the other fragments in my Drawer Activity

  1. I tried to print a Log in onDestroy of the Drawer Activity, but nothing is printed.
  2. I tried to print a log in onActivityCreated of the ReportFragment (the one who is "active" during the Camera Intent).
  3. I tried to put in the manifest android: configChanges = "orientiation | screenSize", but this had no effect

    public class ReportFragment extends Fragment {
    Integer REQUEST_CAMERA = 1, SELECT_FILE = 0;
    private static final int CAMERA_PHOTO = 111;
    private Uri imageToUploadUri;
    private File f;
    ImageView ivImage;
    Intent chooserIntent = null;
    
    private void SelectImage(){
    
        final CharSequence[] items={"Camera","Galleria", "Cancella"};
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Aggiungi immagine");
    
        builder.setItems(items, new DialogInterface.OnClickListener() {
    
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (items[i].equals("Camera")) {
                    if(f!= null){
                        f.delete();
                    }
                    chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    f = new File(Environment.getExternalStorageDirectory(), "POST_IMAGE.jpg");
                    imageToUploadUri = Uri.fromFile(f);          
                    startActivityForResult(chooserIntent, 1);
                } else if (items[i].equals("Galleria")) {
    
                    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(intent.createChooser(intent,"Select File"), 2);
    
                } else if (items[i].equals("Cancella")) {
                    dialogInterface.dismiss();
                }
            }
        });
        builder.show();
    
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                if (imageToUploadUri != null) {   
                    Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
                    if (reducedSizeBitmap != null) {
                        textPhoto.setText("Foto allegata con successo!");
                        textPhoto.setTextColor(Color.parseColor("#008000"));
                        ivImage.setImageBitmap(reducedSizeBitmap);
                        flagImage = true;
                    }
                    else {
                        Toast.makeText(getActivity(), "Error while capturing Image", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(getActivity(), "Error while capturing Image", Toast.LENGTH_LONG).show();
                }
            } else if (requestCode == 2) {
    
                Uri selectedImageUri = data.getData();
                textPhoto.setText("Foto allegata con successo!");
                textPhoto.setTextColor(Color.parseColor("#008000"));
                ivImage.setImageURI(selectedImageUri);
                flagImage = true;
            }
        }
    }
    
    private Bitmap getBitmap(String path) {
    
        Uri uri = Uri.fromFile(new File(path));
        InputStream in = null;
        try {
            final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
            in = getActivity().getApplicationContext().getContentResolver().openInputStream(uri);
    
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(in, null, o);
            in.close();
    
    
            int scale = 1;
            while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
                    IMAGE_MAX_SIZE) {
                scale++;
            }
            Log.d("", "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);
    
            Bitmap b = null;
            in = getActivity().getApplicationContext().getContentResolver().openInputStream(uri);
            if (scale > 1) {
                scale--;
                // scale to max possible inSampleSize that still yields an image
                // larger than target
                o = new BitmapFactory.Options();
                o.inSampleSize = scale;
                b = BitmapFactory.decodeStream(in, null, o);
    
                // resize to desired dimensions
                int height = b.getHeight();
                int width = b.getWidth();
                Log.d("", "1th scale operation dimenions - width: " + width + ", height: " + height);
    
                double y = Math.sqrt(IMAGE_MAX_SIZE
                        / (((double) width) / height));
                double x = (y / height) * width;
    
                Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
                        (int) y, true);
                b.recycle();
                b = scaledBitmap;
    
                System.gc();
            } else {
                b = BitmapFactory.decodeStream(in);
            }
            in.close();
    
            Log.d("", "bitmap size - width: " + b.getWidth() + ", height: " +
                    b.getHeight());
            return b;
        } catch (IOException e) {
            Log.e("", e.getMessage(), e);
            return null;
        }
    }
    

I get no error in the debugger just a message about a certain skia thread

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Marco
  • 11
  • 2
  • You need to learn a bit more about the Android activity lifecycle, mostly just where to save temporary data (hint: Bundles) – Zun Jul 03 '19 at 09:28
  • here, this can help you https://stackoverflow.com/questions/151777/how-to-save-an-android-activity-state-using-save-instance-state – Mansoor Ali Jul 03 '19 at 09:49
  • Hi, I tried to put a Log in onActivityCreated but nothing was printed when I returned from the Camera – Marco Jul 03 '19 at 10:22

0 Answers0