在手机中保存位图

时间:2015-08-09 19:27:33

标签: android bitmap imageview save

我正在开发一种图像编辑器,我将最终图像保存到手机有问题。我尝试过下面的代码,但是当我保存一次并再次更改图像视图并按下保存按钮我遇到问题时,出现了问题:我按下了保存按钮但新图像没有保存,只是我第一次保存的图像再次按下保存按钮保存。我不知道该怎么办请帮助我。

    save.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            img1.buildDrawingCache();
            Bitmap bp = img1.getDrawingCache();

            String root = Environment.getExternalStorageDirectory().toString();
                File newDir = new File(root + "/insta/pics");    
                newDir.mkdirs();
                Random gen = new Random();
                 int n = 10000;
                n = gen.nextInt(n);
                String fotoname = "instaPhoto-"+n+".jpg";
               File file = new File (newDir, fotoname);
                if (file.exists ()) file.delete (); 
                    try {
                        FileOutputStream out = new FileOutputStream(file);
                        bp.compress(Bitmap.CompressFormat.JPEG, 100, out);
                       out.flush();
                       out.close();
                       Toast.makeText(getApplicationContext(), " /insta/pics  ", Toast.LENGTH_SHORT ).show();

                    } catch (Exception e) {

                    }

             }
    });

2 个答案:

答案 0 :(得分:0)

我认为您已运行此代码newDir.mkdirs();

答案 1 :(得分:0)

试试这个;
在file.delete()之后添加这个; file.createNewFile();

相关问题