将位图另存为jpeg图像

马克·马克杜(Mark Mamdouh)

在我的android应用程序中,我生成一个qr代码,然后将其另存为jpeg图像,我使用以下代码:

imageView = (ImageView) findViewById(R.id.iv);
final Bitmap bitmap = getIntent().getParcelableExtra("pic");
imageView.setImageBitmap(bitmap);
save = (Button) findViewById(R.id.save); 
save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            String path = Environment.getExternalStorageDirectory().toString();

            OutputStream fOutputStream = null;
            File file = new File(path + "/Captures/", "screen.jpg");
            if (!file.exists()) {
                file.mkdirs();
            }

            try {

                fOutputStream = new FileOutputStream(file);

                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);

                fOutputStream.flush();
                fOutputStream.close();
                MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return;
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }
    });

但它总是在行上捕获异常:

fOutputStream = new FileOutputStream(file);

是什么引起了这个问题???

桑耶夫·萨哈(Sanjeev Saha)

是什么引起了这个问题???

该语句file.mkdirs();以名称创建目录screen.jpgFileOutputStream不能创建名称的文件screen.jpg,同时还有一个目录该名称被发现。所以你得到了:

java.io.FileNotFoundException

您能否替换以下代码段:

File file = new File(path + "/Captures/", "screen.jpg");
if (!file.exists()) {
   file.mkdirs();
}

通过以下片段:

String dirPath = path + "/Captures/";       
File dirFile = new File(dirPath);
if(!dirFile.exists()){
   dirFile.mkdirs();
}
File file = new File(dirFile, "screen.jpg");

看看结果吗?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章