【发布时间】:2020-08-09 06:19:29
【问题描述】:
我遵循了Saving an image from ImageView into internal storage 的答案,但我仍然无法保存任何东西。我的代码在这里:
public void buttonPickImage(View view) {
FileOutputStream fos;
bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
Random rng = new Random();
int n = rng.nextInt(1000);
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/BAC");
bool = dir.mkdir();
File file = new File(dir, "BAC_"+n+".jpg");
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(),"Image sauvegardée"+bool,Toast.LENGTH_SHORT).show();
}catch (java.io.IOException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(),"IOException: " + e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
通过这种方法,我得到带有消息的 IOExeception :java.io.FileNotFoundException: /storage/emulated/0/BAC/BAC_396.jpg: open failed: ENOENT (No such file or directory)
我也尝试将其保存到内部存储,但它不适合我: https://www.tutorialspoint.com/how-to-write-an-image-file-in-internal-storage-in-android 使用这种方法,程序运行但布尔 mkdir 给我 false。
谢谢你帮助我
【问题讨论】:
-
dir.mkdir();仅当目录不存在时才调用 mkdir。如果您检查返回值。如果它返回 false,则显示一个 toast 以通知用户。并返回。不要继续,因为如果目录不存在,那将毫无意义。请尝试并在此处调整您的代码。 -
您没有知道显示的是哪个吐司。那么我们怎么知道呢?
-
"not working"。那应该是"IOException: " + e.getMessage(),以便更好地通知用户。 -
您也没有从 logcat 中发布相关行。您可能有 IOException 但没有通知我们。
-
to internal storage but its not working for me那么发生了什么?有什么错误?