【问题标题】:Android Saving a Bitmap in a Folder -Duplicates-Android 在文件夹中保存位图 -Duplicates-
【发布时间】:2013-01-01 08:37:25
【问题描述】:

我的问题是每当我保存位图时。我将一个保存在我想要的文件夹(MyFolder)中,一个保存在 DCIM/Camera 中。我没有看到任何使其保存在 DCIM 目录中的代码?

这是我的代码...

    case R.id.menu_save:
        try {

            String path = Environment.getExternalStorageDirectory()
                    .toString();
            File myNewFolder = new File(path + "/MyFolder");
            myNewFolder.mkdirs();
            OutputStream fOut = null;
            File file = new File(path, "/MyFolder/HK" + filename + ".jpg");
            fOut = new FileOutputStream(file);

            newBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
            fOut.flush();
            fOut.close();

            MediaStore.Images.Media.insertImage(getContentResolver(),
                    file.getAbsolutePath(), file.getName(), file.getName());

            Toast.makeText(getApplicationContext(),
                    filename + "Has been saved!", Toast.LENGTH_LONG)
                    .show();

        } catch (Exception e) {

            Toast.makeText(
                    getApplicationContext(),
                    "Problem to Save the File", Toast.LENGTH_LONG).show();
        }

        break;

【问题讨论】:

  • 我相信MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(), file.getName(), file.getName()) 正在读取您通过fout 保存的图像到您选择的路径并将其写入DCIM 文件夹
  • MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath()‌​, file.getName(), file.getName()) 没错,当我删除该代码时问题解决了。如果您写为答案,我可以接受作为答案。谢谢。

标签: android file bitmap save


【解决方案1】:

在@Dixit 链接的线程中,您可以指定文件路径URI

File fileDir = new File(Environment.getExternalStorageDirectory() +
    "/saved_images");
fileDir.mkdirs();

File file = new File(fileDir, "image.jpg");

Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

这样相机会将图像保存到指定路径而不是 DCIM 文件夹。

编辑:您必须事先在 sdcard 上创建文件夹,也许这就是问题所在。否则,这应该可以工作。

【讨论】:

  • 它保存我想要的目录没问题。但它也保存了 DCIM 文件夹,但我的问题已解决,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多