应用程序的主要目标是使用URL从网络图像下载。现在图像下载很好,URL链接运行良好。但是将图像保存到设备存储时会出现问题。 我的手机有SD卡,保存效果很好。 我的平板电脑已经存储了16Gb的存储空间。在我的平板电脑上,图像是下载但未保存。
保存图片的源代码示例:
FileManager fileManager = new FileManager();
if (fileManager.fileExists(filename)) {
File file = fileManager.getFile(filename);
Uri fileUri = Uri.fromFile(file);
return new SaveImageRequest(fileUri);
} else {
String type = ".png"; //fallback to ".png"
if (path.toString().lastIndexOf(".") != -1) { //-1 means there are no punctuations in the path
type = path.toString().substring(path.toString().lastIndexOf("."));
}
DownloadManager.Request request = new DownloadManager.Request(path);
request.setTitle(notificationTitle);
request.setVisibleInDownloadsUi(false);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.allowScanningByMediaScanner();
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, "/DestinationDir/" + filename + type);
return new SaveImageRequest(downloadManager.enqueue(request));
}
您是否有任何解决方案可以在具有内置存储设备和带外部存储设备的设备上保存图像? PS:用于访问存储的android权限
答案 0 :(得分:0)
您应该按照the Meta post
中的说明检查媒体可用性/* 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;
}