i am trying to make a fragment that is having a layout with multiple buttons, on each button, we can capture different images and save them into gallery. i want to capture image on 1st click and on 2nd click on the same button i want to display the same image that has been saved with custom name into gallery. **when i reopen the gallery it asks again to capture the image, but i want to only display the image when it has already been clicked last time

enter code here** btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // if(uri.equals(uri)) clickcount1 = clickcount1 + 1; if (clickcount1 == 1) {

                    Intent in = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
                   // SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");

                    String datetime = sdf.format(new Date(System.currentTimeMillis()));
                    Log.i("DateTime", datetime);
                    //String datetime = DateFormat.getTimeInstance().format(new Date()).toString();
                    File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "MY_Docs");
                    if (!folder.exists())
                        folder.mkdirs();
                    File file = new File(folder, "Aadhar_" + datetime + ".jpg");
                    try {
                        file.createNewFile();
                    }
                    catch (Exception e) {
                        Log.e("creating file exception", e.toString());
                    }
                    uriaadhar = Uri.fromFile(file);
                    Log.i("URI after file creation", uriaadhar.toString());
                    Toast.makeText(getActivity(), "After file conversion to uri", Toast.LENGTH_SHORT).show();
                    in.putExtra(MediaStore.EXTRA_OUTPUT, uriaadhar);
                    startActivity(in);
                }

            if  (clickcount1 >= 2 )
            {
                    String filepath = uriaadhar.getPath();
                    Log.e("FilePath2", filepath);
                    AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
                    View v = inflater.inflate(R.layout.doc1, null);
                    ab.setView(v);
                    ab.setTitle("Aadhar Card");
                    ImageView ivAadhar = (ImageView) v.findViewById(R.id.iv_Aadhar);
                    Bitmap bitmap=BitmapFactory.decodeFile(filepath);
                //Code for marshmallow to display image in imageview
                int nh = (int) ( bitmap.getHeight() * (512.0 / bitmap.getWidth()) );
                Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 600, nh, true);

                    ivAadhar.setImageBitmap(scaled);
                    //ivAadhar.setImageBitmap(BitmapFactory.decodeFile(filepath));
                    ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
                    ab.show();

            }

        }

    });
1

There are 1 best solutions below

2
On

Make your click count variable static

private static int clickcount1 = 0;

Or you can save the state of your mode in to sharedPeference so you can reuse it whenever your want.