【发布时间】:2019-10-02 09:33:09
【问题描述】:
我有一个应用程序,它可以从 URL 下载所有图像并将其保存到设备中的隐藏文件夹中。如果手机有外部 SD 卡,它可以工作,否则应用程序崩溃。所以我正在尝试将我的代码转换为将图像存储在内存中。
尽管我阅读了许多现有问题,但我无法解决我的问题。我不知道它是否可能是我的“request.setAllowedNetworkTypes”中的“.setDestinationInExternalPublicDir”导致问题的原因,可以吗?我应该如何更改我的代码?
我也试过用getDataDirectory或者getFilesDir()但是还是有异常:
java.lang.IllegalStateException: 无法创建目录
这是我当前适用于具有外部 SD 卡的手机的代码:
public class ImagesDownloader {
private static File imageDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + ".Photo");
//or private static File imageDirectory;
public static void downloadImages(Context context, List<MyList> imageList) {
//here I tried to change the code like that (without success):
//File imageDirectory = new File(Environment.getDataDirectory() + File.separator + ".Photo");
//or
//File imageDirectory = new File(context.getFilesDir() + File.separator + ".Photo");
if (!imageDirectory.exists()) {
if (!imageDirectory.mkdirs()) {
Log.d("App", "failed to create directory");
}
}
if(getImageFolderSize()==0){
Iterator<MyList> iterator = imageList.iterator();
while (iterator.hasNext()) {
MyList MyList = iterator.next();
String fileName = MyList.getFilename();
String imgurl = "https://.../media/MyList/" + fileName;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
File imageFile = new File(Environment.getExternalStoragePublicDirectory(
imageDirectory.getPath()) + "/" + fileName);
//and here how I wanted to change the code (without success):
//File imageFile = = new File(imageDirectory.getPath() + "/" + fileName);
if (!imageFile.canRead()) {
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imgurl));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(fileName)
.setDescription("file description")
.setDestinationInExternalPublicDir(imageDirectory.getPath(), fileName)
.setVisibleInDownloadsUi(true);
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
}
};
context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
if (downloadManager != null) {
downloadManager.enqueue(request);
}
}
}
}
}
public static BitmapDescriptor getImgefromDeviceFolder(String fileName){
File imageFile = new File(Environment.getExternalStoragePublicDirectory(
imageDirectory.getPath()) + "/" + fileName);
if (imageFile.canRead()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
return BitmapDescriptorFactory.fromBitmap(myBitmap);
}
return null;
}
static int getImageFolderSize(){
File directory = Environment.getExternalStoragePublicDirectory(imageDirectory.getPath());
if(directory.exists()){
File[] files = directory.listFiles();
if(files!=null){
return files.length;
}
}
return 0;
}
}
如果我犯了一些大错误,我希望你能原谅我,这是我第一次做档案归档工作
【问题讨论】:
-
我的问题有点不同,因为我正在尝试将图像存储在内部存储...
-
我已经设置了你发给我的讨论中提到的权限,如果手机有外置SD卡,一切正常。也许我应该改变我的问题的标题
标签: java android file android-internal-storage