如何在android中保存另一个图像?

时间:2016-10-24 04:33:43

标签: android image android-studio uiimageview

        save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View b) {
            if ( ( d1.getText().toString().trim().equals("") ) && d2.getText().toString().trim().equals("")
                    && d3.getText().toString().trim().equals("") && d4.getText().toString().trim().equals("") && d5.getText().toString().trim().equals("")
                    && d6.getText().toString().trim().equals("") )
            {
                Toast.makeText(getApplicationContext(), "Generate before Save", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Your Qr Image Has Been Save in /DCIM/Camera ", Toast.LENGTH_SHORT).show();

                // TODO Auto-generated method stub
                //attempt to save the image

                b = findViewById(R.id.image);
                b.setDrawingCacheEnabled(true);
                Bitmap bitmap = b.getDrawingCache();
                File file = new File("/DCIM/Camera/");
                File root = Environment.getExternalStorageDirectory();
                File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpeg");
                try {
                cachePath.createNewFile();
                FileOutputStream ostream = new FileOutputStream(cachePath);

                 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
                    ostream.flush();
                    ostream.close();


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    });


    }





}

无论如何都要从我的图像视图中保存另一个图像而不是覆盖以前的图像?顺便说一下这段代码的工作方式,但我不能用另一个名字保存另一个图像。有人能帮助我吗?

4 个答案:

答案 0 :(得分:0)

您为所有图片File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpeg");指定了相同的名称,只需更改它或在最后附加时间戳。

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image" + timeStamp + ".jpeg");

答案 1 :(得分:0)

应该改变

File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpeg");

可能解决

使用自定义editText创建新的警报对话框,在editText中保存图像的名称,将其设置为字符串值

String renmae=editText.getText().toString();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/" + renmae+ ".jpeg");

其他使用时间戳保存名称

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image" + timeStamp + ".jpeg");

答案 2 :(得分:0)

使用此代码:

String imageName="image-"+System.currentTimeMillis()+".jpeg";

File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/"+imageName);

所有时候,图像名称都不同。

答案 3 :(得分:0)

save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View b) {
            if ( ( d1.getText().toString().trim().equals("") ) && d2.getText().toString().trim().equals("")
                    && d3.getText().toString().trim().equals("") && d4.getText().toString().trim().equals("") && d5.getText().toString().trim().equals("")
                    && d6.getText().toString().trim().equals("") )
            {
                Toast.makeText(getApplicationContext(), "Generate before Save", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Your Qr Image Has Been Save in /DCIM/Camera ", Toast.LENGTH_SHORT).show();

                // TODO Auto-generated method stub
                //attempt to save the image

                b = findViewById(R.id.image);
                b.setDrawingCacheEnabled(true);
                Bitmap bitmap = b.getDrawingCache();
                File file = new File("/DCIM/Camera/");
                File root = Environment.getExternalStorageDirectory();
                File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image_"+System.currentTimeMillis()+".jpeg");
                try {
                cachePath.createNewFile();
                FileOutputStream ostream = new FileOutputStream(cachePath);

                 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
                    ostream.flush();
                    ostream.close();
        MediaStore.Images.Media.insertImage(getContentResolver(),cachePath.getAbsolutePath(),cachePath.getName(),cachePath.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    });

    }