这就是我尝试这样做的方式:
public void SaveImageToMemory(Bitmap bitmap, String fileName){
File outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ImageEncryptionDemo", fileName+".jpeg");
try {
outFile.getParentFile().mkdirs();
outFile.createNewFile();
FileOutputStream outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Log.i("app", "Saved image to " + outFile.getAbsolutePath());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (java.io.IOException e){
e.printStackTrace();
}
}
我不知道为什么它会抛出IOException,即使:
outFile.getParentFile().mkdirs();
应确保创建所有必需的父目录true
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
AndroidManifest中的权限:
<manifest [...]>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
答案 0 :(得分:0)
在第二行,你刚刚感到困惑。看看这个。
^ Cannot assign `4` to `test` because number [1] is incompatible with string [2].
在
File outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/ImageEncryptionDemo/" + fileName+".jpeg");
您必须使用+而不是逗号。