Android将位图写入SD卡

发布于 2024-11-29 01:02:10 字数 1086 浏览 9 评论 0原文

我试图在 Android 上将位图写入 SD 卡,但收到错误消息
/mnt/sdcard/PhysicsSketchpad/sketchpad145.png(没有这样的文件或目录)。
我在清单中声明了 android.permission.WRITE_EXTERNAL_STORAGE 权限,这是我的代码:

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                        "/PhysicsSketchpad/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "sketchpad" + pad.t_id + ".png");
FileOutputStream fOut = new FileOutputStream(file);

bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();

发生了什么事?

更新

似乎当我尝试写入现有目录时,我收到权限被拒绝错误,

08-11 09:55:23.796: WARN/Physics Sketchpad(8881): Error when saving: IOException /mnt/sdcard/download/sketchpad54.png (Permission denied)

当我尝试保存在新目录中时,我出现没有此类文件或目录的错误,
08-11 09:59:20.175: WARN/Physics Sketchpad(9040): 保存时出错:IOException /mnt/sdcard/PhysicsSketchpad/sketchpad55.png (没有这样的文件或目录)

另外,File.mkdirs()根据是否成功返回一个布尔值,并且它返回错误的。

I'm trying to write a bitmap to an sdcard on android, and I get the error message of
/mnt/sdcard/PhysicsSketchpad/sketchpad145.png (No such file or directory).
I declared the android.permission.WRITE_EXTERNAL_STORAGE permission in the manifest, and this is my code:

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                        "/PhysicsSketchpad/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "sketchpad" + pad.t_id + ".png");
FileOutputStream fOut = new FileOutputStream(file);

bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();

What's going on?

UPDATE

It seems as though when I try to write to an existing directory, I get an permission denied error,

08-11 09:55:23.796: WARN/Physics Sketchpad(8881): Error when saving: IOException /mnt/sdcard/download/sketchpad54.png (Permission denied)

and when I try to save in a new directory I get a no such file or directory error,
08-11 09:59:20.175: WARN/Physics Sketchpad(9040): Error when saving: IOException /mnt/sdcard/PhysicsSketchpad/sketchpad55.png (No such file or directory)

In addition, File.mkdirs() returns a boolean based on if it succeeded or not, and it returned false.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一直在等你来 2024-12-06 01:02:10

试试这个代码。

  String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                            "/PhysicsSketchpad";
    File dir = new File(file_path);
if(!dir.exists)
    dir.mkdirs();
    File file = new File(dir, "sketchpad" + pad.t_id + ".png");
    FileOutputStream fOut = new FileOutputStream(file);

    bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
    fOut.flush();
    fOut.close();

try this code.

  String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                            "/PhysicsSketchpad";
    File dir = new File(file_path);
if(!dir.exists)
    dir.mkdirs();
    File file = new File(dir, "sketchpad" + pad.t_id + ".png");
    FileOutputStream fOut = new FileOutputStream(file);

    bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
    fOut.flush();
    fOut.close();
爱给你人给你 2024-12-06 01:02:10

试试这个代码。这对我有用。

public void saveBitmap(Bitmap bm)
{
    try
    {
        String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/";
        String mFilePath = mBaseFolderPath + "abcd.jpg";

        FileOutputStream stream = new FileOutputStream(mFilePath);
        bm.compress(CompressFormat.JPEG, 100, stream);
        stream.flush();
        stream.close();
    }
    catch(Exception e)
    {
        Log.e("Could not save", e.toString());
    }
}

沙什

try this code. This has worked for me.

public void saveBitmap(Bitmap bm)
{
    try
    {
        String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/";
        String mFilePath = mBaseFolderPath + "abcd.jpg";

        FileOutputStream stream = new FileOutputStream(mFilePath);
        bm.compress(CompressFormat.JPEG, 100, stream);
        stream.flush();
        stream.close();
    }
    catch(Exception e)
    {
        Log.e("Could not save", e.toString());
    }
}

Shash

柏林苍穹下 2024-12-06 01:02:10

您获取绝对路径检查错误,您获取路径

/mnt/sdcard/AppName/appname145.png

并将位置设置

Environment.getExternalStorageDirectory().getAbsolutePath() + 
                        "/PhysicsSketchpad/";

为“PhysicsSketchpad”目录未进入上述路径

尝试此操作

Environment.getExternalStorageDirectory().toString()+ "/PhysicsSketchpad/";

you getting the absolute path check into the error what you getting the path

/mnt/sdcard/AppName/appname145.png

and you set the

Environment.getExternalStorageDirectory().getAbsolutePath() + 
                        "/PhysicsSketchpad/";

where as "PhysicsSketchpad" dir not getting in above path

try this

Environment.getExternalStorageDirectory().toString()+ "/PhysicsSketchpad/";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文