【问题标题】:write image in external directory android在外部目录android中写入图像
【发布时间】:2016-08-26 05:09:29
【问题描述】:

如何将图像写入电话目录。目录已创建,但我无法在此文件夹中写入图像。这是我的代码。请检查我是否做错了提前谢谢。

           Bitmap bitmap;
          String directory = new_path; // I am getting path here of image like sdcard/0/emulated/image.jpg
           String folder_name = "abc"

            bitmap = BitmapFactory.decodeFile(directory);

            iv_6.setImageBitmap(bitmap); // image displayed here but not saving in directory.

            try {

                File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator +folder_name);
                f.mkdirs();

                FileOutputStream fo = new FileOutputStream(f);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 40, fo);
                fo.close();

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

【问题讨论】:

  • 你需要在文件夹名称后面加上带扩展名的文件名。

标签: android file-handling


【解决方案1】:
public void savePng(Bitmap bitmap, String filePath) {
    try {
        File temp = new File(filePath);
        FileOutputStream os = new FileOutputStream(temp + ".png");
        bitmap.compress(Bitmap.CompressFormat.PNG, 50, os);
        os.flush();
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 我试过了,但我没有在电话目录中获取图像
  • 能发一下logcat吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多