7

我正在尝试将图像文件写入特定目录中的公共画廊文件夹,但我不断收到错误消息,因为它是一个目录,所以我无法打开该文件。

到目前为止我所拥有的是以下

//set the file path
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;

    File outputFile = new File(path,"testing.png");


    outputFile.mkdirs();

    FileOutputStream out = new FileOutputStream(outputFile);
    bmp.compress(Bitmap.CompressFormat.PNG, 100, out);

其中目录是应用程序名称。所以应用程序保存的所有照片都将进入该文件夹/目录,但我不断收到错误消息

/storage/sdcard0/Pictures/appname/testing.png: open failed: EISDIR (Is a directory)

即使我不尝试将其放在目录中并将变量路径转换为 ​​File 之类的

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

我没有收到错误,但是照片仍然没有出现在画廊中。

***Answer 问题是,当我最初运行此代码时,它创建了一个名为 testing.png 的目录,因为在目录中创建文件之前我未能创建目录。所以解决方案是先制作目录,然后用一个单独的文件写入它,就像这样

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + directory;

//directory is a static string variable defined in the class

            //make a file with the directory
    File outputDir = new File(path);

            //create dir if not there
    if (!outputDir.exists()) {
         outputDir.mkdir();

    }

            //make another file with the full path AND the image this time, resized is a static string
    File outputFile = new File(path+File.separator+resized);

    FileOutputStream out = new FileOutputStream(outputFile);
    bmp.compress(Bitmap.CompressFormat.PNG, 100, out);

请注意,如果您犯了与我开始时相同的错误,您可能需要进入存储并手动删除目录

4

6 回答 6

13

您正在尝试写入目录而不是文件。试试这个

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;   
File outputDir= new File(path);   
outputDir.mkdirs();   
File newFile = new File(path + File.separator + "test.png");
FileOutputStream out = new FileOutputStream(newFile);   
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);   
于 2012-10-19T03:56:41.807 回答
3

您的代码是正确的,只需进行少量更改,如下所示,

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;

    // First Create Directory
    File outputFile = new File(path);
    outputFile.mkdirs();

    // Now Create File
    outputFile = new File(path,"testing.png");
    FileOutputStream out = new FileOutputStream(outputFile);
    bmp.compress(Bitmap.CompressFormat.PNG, 100, out);

另外不要忘记在您的 AndroidManifest.xml 文件中授予 WRITE_EXTERNAL_STORAGE 权限。

于 2012-10-19T03:59:15.957 回答
2

如果您在使用 Android Emulator 时遇到此错误;您需要在模拟器上启用 SD 卡存储。

于 2015-04-19T19:20:13.507 回答
2
public static String SaveImage(Bitmap finalBitmap) {

        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        File myDir = new File(root + "/FolderName");
        if(!myDir.exists())
            myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".png";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

        } catch (Throwable e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }
于 2018-01-02T18:22:32.413 回答
2

getExternalStoragePublicDirectory 现在已弃用,您应该使用

context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
于 2020-05-11T16:50:18.750 回答
0

使用这种方式:

bitmap.compress(CompressFormat.JPEG, 100,  new FileOutputStream("/mnt/sdcard/" + new Date().getTime() + ".jpg"));`

Path : Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + file name

于 2012-10-19T03:58:46.583 回答