【问题标题】:Android Unable to create directory on Internal StorageAndroid无法在内部存储上创建目录
【发布时间】: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


【解决方案1】:

此代码适用于一张图片,并且可以正常工作。

您可以对其进行测试并为您的图像列表进行更改。

  public static void downloadImages(Context context, String fileName ) {
    String storagePath = Environment.getExternalStorageDirectory()
        .getPath()
        + "/Directory_name/";
    //Log.d("Strorgae in view",""+storagePath);
    File f = new File(storagePath);
    if (!f.exists()) {
      f.mkdirs();
    }
    //storagePath.mkdirs();
    String pathname = f.toString();
    if (!f.exists()) {
      f.mkdirs();
    }
    //                Log.d("Storage ",""+pathname);
    DownloadManager dm = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
    String imgurl = "https://.../media/MyList/" + fileName;
    Uri uri = Uri.parse(imgurl);
      DownloadManager.Request request = new DownloadManager.Request(uri);
      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

      request.setDestinationInExternalPublicDir("", uri.getLastPathSegment());
      Long referese = dm.enqueue(request);

      Toast.makeText(context, "Downloading...", Toast.LENGTH_SHORT).show();

  }

【讨论】:

  • 不幸的是,我现在有 java.lang.IllegalStateException:无法创建目录:/storage/emulated/0/storage/emulated/0/.Photo
猜你喜欢
  • 2013-06-06
  • 2022-08-19
  • 2012-11-23
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
相关资源
最近更新 更多