【问题标题】:How to create a custom directory in internal storage to save the downloaded file?如何在内部存储中创建自定义目录来保存下载的文件?
【发布时间】:2019-06-08 03:31:00
【问题描述】:

我正在使用 firebase 开发应用。我已手动将 pdf 文件上传到 firebase 存储中,以便用户可以使用该应用程序下载它。下载的文件存储在 Android -> 数据 -> 文件名 -> 目录 -> 文件中。但是我需要一个自定义目录来将文件直接保存在内部存储中。

   public void downloadFile(){
        str = FirebaseStorage.getInstance().getReference();
        final File mydir = this.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;

    ref = str.child("AI (presentation).pptx");
        ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                String url = uri.toString();
                downloadFiles(MainActivity.this,"AI",".ppt",mydir ,url);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
        });
}

public void downloadFiles(Context context, String fileName, String fileExtension, File destinationDirectory, String url) {

    DownloadManager downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(uri);

    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalFilesDir(context, "" + destinationDirectory, fileName + fileExtension);

    downloadmanager.enqueue(request);
}

【问题讨论】:

    标签: android firebase firebase-storage android-internal-storage


    【解决方案1】:

    您不需要使用 DownloadManager。 取而代之的是,您可以使用 StorageReferenece.getBytes() 并手动保存文件。

        private static final long DOWNLOAD_LIMIT = 1024 * 1024; // you can change this
    
        public void downloadFile(){
            StorageReference ref = FirebaseStorage.getInstance().getReference("AI (presentation).pptx");
            ref.getBytes(DOWNLOAD_LIMIT).addOnSuccessListener(new OnSuccessListener<byte[]>() 
            {
                @Override
                public void onSuccess(byte[] bytes) {
                    final String path = Environment.getExternalStorageDirectory().getAbsolutePath()
                        + "/mydir/AI (presentation).pptx";
                    try {
                        writeToFile(bytes, path);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    Log.e(TAG, "fail to download: " + exception);
                }
            });
        }
    
        public void writeToFile(byte[] data, String fileName) throws IOException{
            FileOutputStream out = new FileOutputStream(fileName);
            out.write(data);
            out.close();
        }
    

    【讨论】:

    猜你喜欢
    • 2011-12-03
    • 2017-12-28
    • 2012-06-08
    • 2023-03-14
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多