在第一次单击打开相机和第二次单击捕获的图像显示。当我重新打开应用程序时,它应该在第一次点击时显示捕获的图像

时间:2016-12-19 18:25:02

标签: android image gallery

我正在尝试制作一个具有多个按钮布局的片段,在每个按钮上,我们可以捕获不同的图像并将它们保存到图库中。我希望在第一次点击时捕获图像,然后在第二次点击同一按钮时,我想显示已使用自定义名称保存到图库中的相同图像。 **当我重新打开画廊时,它再次请求捕捉图像,但我想仅在上次点击时显示图像

enter code here ** btn.setOnClickListener(new View.OnClickListener(){             @覆盖             public void onClick(查看视图){                    // 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 个答案:

答案 0 :(得分:1)

使点击次数变为静态

private static int clickcount1 = 0;

或者您可以将模式的状态保存到sharedPeference中,这样您就可以随时重复使用它。