当我保存我的位图时,我需要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
我拥有我需要的用户权限()但无论如何它都无效。有什么问题?
答案 0 :(得分:2)
使用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;
}
答案 1 :(得分:2)
您是否包含正确的权限?您需要写入外部存储权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
修改强>
尝试将文件放在SDCARD的目录中,例如:
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test" + File.Separator + "mind");
答案 2 :(得分:1)
加载听起来像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();
}
}