保存位图 - IO 异常

发布于 2025-01-01 02:41:22 字数 1231 浏览 2 评论 0原文

当我保存位图时,它会将我带到 IOExeption,为什么?我不知道:

    static Bitmap[] bmp = new Bitmap[4];

public static void save(FileIO files, int location) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    try {
        File f = new File(Environment.getExternalStorageDirectory() 
                  + File.separator + "test" + File.separator + "mind");
        Log.d("path", f.toString());
        f.createNewFile();
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        bmp[location].compress(Bitmap.CompressFormat.PNG, 100, bytes);
        fo.write(bytes.toByteArray());
        fo.close();
        Log.d("fisnish", "Bitmap saved");
    } catch (IOException e) {
        Log.d("IO", e.getMessage());
    }
}

保存时的 Logcat:

01-29 00:53:58.020: DEBUG/path(8222): /mnt/sdcard/test/mind
01-29 00:53:58.060: DEBUG/IO(8222): Permission denied
01-29 00:53:58.240: DEBUG/dalvikvm(8222): GC_EXTERNAL_ALLOC freed 7K, 53% free 2563K/5379K, external 5375K/6713K, paused 178ms
01-29 00:54:02.029: DEBUG/dalvikvm(4723): GC_EXPLICIT freed 7K, 53% free 2643K/5511K, external 1625K/2137K, paused 3421ms

我拥有我需要的用户权限() 但无论如何它都不起作用。有什么问题吗?

When I save my bitmap, it takes me to IOExeption, why? I dont know:

    static Bitmap[] bmp = new Bitmap[4];

public static void save(FileIO files, int location) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    try {
        File f = new File(Environment.getExternalStorageDirectory() 
                  + File.separator + "test" + File.separator + "mind");
        Log.d("path", f.toString());
        f.createNewFile();
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        bmp[location].compress(Bitmap.CompressFormat.PNG, 100, bytes);
        fo.write(bytes.toByteArray());
        fo.close();
        Log.d("fisnish", "Bitmap saved");
    } catch (IOException e) {
        Log.d("IO", e.getMessage());
    }
}

Logcat when I save:

01-29 00:53:58.020: DEBUG/path(8222): /mnt/sdcard/test/mind
01-29 00:53:58.060: DEBUG/IO(8222): Permission denied
01-29 00:53:58.240: DEBUG/dalvikvm(8222): GC_EXTERNAL_ALLOC freed 7K, 53% free 2563K/5379K, external 5375K/6713K, paused 178ms
01-29 00:54:02.029: DEBUG/dalvikvm(4723): GC_EXPLICIT freed 7K, 53% free 2643K/5511K, external 1625K/2137K, paused 3421ms

I have the user permission that I need() But it doesn't work anyway. What's the problem?

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

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

发布评论

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

评论(3

爱给你人给你 2025-01-08 02:41:22

通过使用 canvas.drawBitmap 并提供矩阵来尝试此缩放位图。

public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
    Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Matrix m = new Matrix();
    m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
    canvas.drawBitmap(bitmap, m, new Paint());

    return output;
}

try this scale bitmaps by using canvas.drawBitmap with providing matrix.

public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
    Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Matrix m = new Matrix();
    m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
    canvas.drawBitmap(bitmap, m, new Paint());

    return output;
}
心凉怎暖 2025-01-08 02:41:22

您是否包含正确的权限?您将需要写入外部存储的权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

编辑

尝试将文件放置在 SDCARD 上的某个目录中,例如:

File f = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "test" + File.Separator + "mind");

Are you including the correct permissions? You will need the write to external storage permission.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Edit

Try placing the file within a directory on the SDCARD, such as:

File f = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "test" + File.Separator + "mind");
不甘平庸 2025-01-08 02:41:22

加载听起来像是 BitmapFactory.decodeFile 的工作。另外,我会直接压缩到您的文件输出流:

public static void save(FileIO files, int location) {
    try {
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "numbers" + location);
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        bmp[location].compress(Bitmap.CompressFormat.PNG, 40, fo);
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Loading sounds like a job for BitmapFactory.decodeFile. Also, I would compress directly to your file output stream:

public static void save(FileIO files, int location) {
    try {
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "numbers" + location);
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        bmp[location].compress(Bitmap.CompressFormat.PNG, 40, fo);
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文