简体   繁体   English

Android 将 bitmap 写入 SD 卡

[英]Android writing bitmap to sdcard

I'm trying to write a bitmap to an sdcard on android, and I get the error message of我正在尝试将 bitmap 写入 android 上的 SD 卡,我收到错误消息
/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:我在清单中声明了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();

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.此外, File.mkdirs()根据是否成功返回 boolean,返回 false。

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();

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沙什

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其中“PhysicsSketchpad”目录没有进入上述路径

try this尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM