【问题标题】:on 1st click opens camera and 2nd click captured image displays. when i reopen the app then it should display captured image on 1st click第一次单击打开相机,第二次单击显示捕获的图像。当我重新打开应用程序时,它应该在第一次单击时显示捕获的图像
【发布时间】:2017-05-04 20:49:21
【问题描述】:

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

enter code here** btn.setOnClickListener(new View.OnClickListener() { @覆盖 public void onClick(查看视图){ // if(uri.equals(uri)) 点击计数1 = 点击计数1 + 1; 如果(点击计数1 == 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();

            }

        }

    });

【问题讨论】:

    标签: android image gallery


    【解决方案1】:

    使您的点击计数变量静态

    private static int clickcount1 = 0;
    

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

    【讨论】:

    • 能否请您解释一下使用代码行 clickcount1=2;我无法理解它..
    • 我对您的问题有误解,只是使用静态作为变量或使用某种数据存储来存储值,因此当您的应用程序再次启动时,它知道哪个按钮已在上一步中拍照。我将编辑答案。
    猜你喜欢
    • 2015-07-23
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2012-09-02
    • 2018-08-17
    • 2012-07-22
    • 1970-01-01
    • 2021-01-05
    相关资源
    最近更新 更多