【问题标题】:saving bitmap - IO Exception保存位图 - IO 异常
【发布时间】:2012-01-28 23:42:19
【问题描述】:

当我保存位图时,它会将我带到 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

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

【问题讨论】:

  • 您发现此代码有任何异常。
  • 是的,当我尝试使用 bmp[i] 时,它给了我一个 NullPointerException
  • 不确定,但你检查过你的文件是否在你的位置。
  • in = new FileInputStream(Environment.getExternalStorageDirectory() + File.separator + "numbers" + i);这是什么?
  • 这里是不是你的位图,你可以手动检查一下吗..

标签: android bitmap save sd-card ioexception


【解决方案1】:

通过使用 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;
}

【讨论】:

    【解决方案2】:

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

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

    编辑

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

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

    【讨论】:

    • 是的,现在当我检查堆栈跟踪时,它代表:“Persission denied”。为什么?
    • 请发布您的 logcat 异常。这将有助于找出问题
    • 另外,您尝试写入的完整路径是什么?它最终会成为 /sdcard/mind 吗?我见过一些情况,您可能需要写入 SDCARD 上的文件夹而不是根目录。
    • 异常:01-29 00:22:42.129:D/IO(8​​025):权限被拒绝
    • 我已经检查了路径,结果应该可以工作:01-29 00:19:44.119: D/path(7994): /mnt/sdcard/mind
    【解决方案3】:

    加载听起来像是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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多