如何使用PRDownloader库下载文件 [英] how to use PRDownloader library to download file

查看:53
本文介绍了如何使用PRDownloader库下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PRDownloader库下载文件,但是我有两个方程式

I want to download file by using PRDownloader library but I have two equations

第一:如何使用该库下载文件?...

first: how to use the library to download file ? ...

秒:我必须在 String dirPath 中输入什么?....

second: what must I put in String dirPath ?....

我的MainActivity

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        PRDownloader.initialize(this);

        PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
                .setDatabaseEnabled(true)
                .build();
        PRDownloader.initialize(getApplicationContext(), config);




        String url = "https://firebasestorage.googleapis.com/v0/b/mohammed-1f35b.appspot.com/o/fear-storynory-jana.mp3?alt=media&token=4d0fc7a3-2b95-4ddd-8ecc-c792a09ab539";
        String fileName ="myPhoto";
        String dirPath = "????????";

        int downloadId = PRDownloader.download(url, dirPath, fileName)
                .build()
                .setOnStartOrResumeListener(new OnStartOrResumeListener() {
                    @Override
                    public void onStartOrResume() {

                    }
                })
                .setOnPauseListener(new OnPauseListener() {
                    @Override
                    public void onPause() {

                    }
                })
                .setOnCancelListener(new OnCancelListener() {
                    @Override
                    public void onCancel() {

                    }
                })
                .setOnProgressListener(new OnProgressListener() {
                    @Override
                    public void onProgress(Progress progress) {

                    }
                })
                .start(new OnDownloadListener() {
                    @Override
                    public void onDownloadComplete() {

                    }

                    @Override
                    public void onError(Error error) {

                    }
                });


        PRDownloader.pause(downloadId);
        PRDownloader.cancel(downloadId);

    }




}

推荐答案

以下是使用PRDownloader下载的示例:

Here is an example how to download with PRDownloader:

private int downloadFile(url){

    String fileName = url.substring(url.lastIndexOf('/')+1);
    String dirPath = getFilesDir().getAbsolutePath()+File.separator+"downloads";
    //String dirPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"downloads";

    return /*download id*/ PRDownloader.download(url, dirPath, fileName)
            .build()
            .start(new OnDownloadListener() {
                @Override
                public void onDownloadComplete() {
                    log("download complete!");
                }
                @Override
                public void onError(Error error) {
                    log("onError: " +error.toString());
                }
            });

}

fileName 是文件一旦下载即存储的内容. dirPath 是存储它的位置.

fileName is what the file will be stored as once downloaded. dirPath is where it will be stored.

如何调用它的示例:

String url = "https://www.your-url.com/files/file.txt"
int downloadId = downloadFile(url);

您可以使用 getFilesDir()将文件存储在内部存储中,也可以使用 getExternalStorageDirectory()存储在外部存储中.

You can store files either on internal storage with getFilesDir(), or external storage with getExternalStorageDirectory().

您不需要任何特殊权限即可存储到内部存储器,但是用户将无法访问数据.如果用户删除了该应用程序,它也会被删除.

You do not need any special permissions to store to internal storage, however the data will not be accessible by the user. It will also be deleted, if the user deletes the app.

如果存储在外部存储中,则需要请求用户的许可.他将能够查看文件(例如,使用文件浏览器).

If you store on external storage, you need to request permission from the user. He will be able to see the files (using a file browser, for example).

这篇关于如何使用PRDownloader库下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆