Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中下载我的SD卡文件夹中的文件_Android_Android Download Manager - Fatal编程技术网

如何在Android中下载我的SD卡文件夹中的文件

如何在Android中下载我的SD卡文件夹中的文件,android,android-download-manager,Android,Android Download Manager,我想从服务器下载文件,对于这项工作,我使用这个库:,并编写以下代码: public class MainActivity extends AppCompatActivity implements DownloadProgressView.DownloadStatusListener { private Button downloadButton, playButton; private long downloadID; private String downloadUR

我想从服务器下载文件,对于这项工作,我使用这个:,并编写以下代码:

public class MainActivity extends AppCompatActivity implements DownloadProgressView.DownloadStatusListener {

    private Button downloadButton, playButton;
    private long downloadID;
    private String downloadURL = "http://cld8.cdn.p30download.com/p30dl-mobile/AppMgr.Pro.III.v4.00.Patched_p30download.com.apk";
    private DownloadManager downloadManager;
    private DownloadProgressView downloadProgressView;

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

        downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        playButton = (Button) findViewById(R.id.playButton);

        final File myDirectory = new File(Environment.getExternalStorageDirectory(), "dirName");

        if(!myDirectory.exists()) {
            myDirectory.mkdirs();
        }

        downloadProgressView = (DownloadProgressView) findViewById(R.id.downloadProgressView);
        downloadButton = (Button) findViewById(R.id.downloadButton);
        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadURL));
                request.setTitle("You garrit");
                request.setDescription("DownloadProgress sample");
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalFilesDir(getApplicationContext(), "myCustomFolderName", "p30download.com.apk");
                request.allowScanningByMediaScanner();

                downloadID = downloadManager.enqueue(request);
                downloadProgressView.show(downloadID, new DownloadProgressView.DownloadStatusListener() {
                    @Override
                    public void downloadFailed(int reason) {

                    }

                    @Override
                    public void downloadSuccessful() {

                        playButton.setVisibility(View.VISIBLE);
                        playButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setDataAndType(Uri.fromFile(getFileUri()), "apk/*");
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void downloadCancelled() {
                        downloadButton.setVisibility(View.VISIBLE);
                    }
                });
                downloadButton.setVisibility(View.GONE);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public File getFileUri() {
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(downloadID);
        Cursor cur = downloadManager.query(query);

        if (cur.moveToFirst()) {
            int columnIndex = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
            if (DownloadManager.STATUS_SUCCESSFUL == cur.getInt(columnIndex)) {
                String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                return new File(Uri.parse(uriString).getPath());
            }
        }

        return null;
    }

    @Override
    public void downloadFailed(int reason) {
        System.err.println("Failed :" + reason);
        downloadButton.setVisibility(View.VISIBLE);
    }

    @Override
    public void downloadSuccessful() {
        playButton.setVisibility(View.VISIBLE);
    }

    @Override
    public void downloadCancelled() {
        downloadButton.setVisibility(View.VISIBLE);
    }
}
我希望下载成功后,将此文件保存到我的文件夹中。对于这项工作,我使用以下代码:

request.setDestinationInExternalFilesDir(getApplicationContext(), "myCustomFolderName", "p30download.com.apk");
但下载完成后,我无法在SD卡中显示myCustomFolder名称,而且我不知道将下载的文件保存在哪里


如何解决此问题?请帮助我,谢谢大家在开始下载之前,请尝试使用
mkdirs
创建文件夹

File file = new File(String path)
file.mkDirs();

mkDirs()
为布尔值,因此您可以检查它是否正确创建文件夹。

开始下载之前,请尝试使用
mkDirs
创建文件夹

File file = new File(String path)
file.mkDirs();
mkDirs()
是布尔值,因此您可以检查它是否正确创建文件夹。

使用库非常简单且易于使用

下载文件的示例代码是

Ion.with(context)
.load("http://example.com/really-big-file.zip")
// have a ProgressBar get updated automatically with the percent
.progressBar(progressBar)
// and a ProgressDialog
.progressDialog(progressDialog)
// can also use a custom callback
.progress(new ProgressCallback() {@Override
   public void onProgress(long downloaded, long total) {
       System.out.println("" + downloaded + " / " + total);
   }
})
.write(new File("/sdcard/really-big-file.zip"))
.setCallback(new FutureCallback<File>() {
   @Override
    public void onCompleted(Exception e, File file) {
        // download done...
        // do stuff with the File or error
    }
});
Ion.with(上下文)
.加载(“http://example.com/really-big-file.zip")
//使用百分比自动更新进度条
.progressBar(progressBar)
//还有一个对话
.progressDialog(progressDialog)
//也可以使用自定义回调
.progress(新ProgressCallback(){@Override
public void onProgress(长时间下载,长时间总计){
系统输出打印项次(“+下载+”/“+总计);
}
})
.write(新文件(“/sdcard/really big File.zip”))
.setCallback(新的FutureCallback(){
@凌驾
未完成公共作废(例外情况e,文件){
//下载完成。。。
//处理文件或错误
}
});
不要忘记添加所需的权限。希望这有帮助:)

使用库非常简单易用

下载文件的示例代码是

Ion.with(context)
.load("http://example.com/really-big-file.zip")
// have a ProgressBar get updated automatically with the percent
.progressBar(progressBar)
// and a ProgressDialog
.progressDialog(progressDialog)
// can also use a custom callback
.progress(new ProgressCallback() {@Override
   public void onProgress(long downloaded, long total) {
       System.out.println("" + downloaded + " / " + total);
   }
})
.write(new File("/sdcard/really-big-file.zip"))
.setCallback(new FutureCallback<File>() {
   @Override
    public void onCompleted(Exception e, File file) {
        // download done...
        // do stuff with the File or error
    }
});
Ion.with(上下文)
.加载(“http://example.com/really-big-file.zip")
//使用百分比自动更新进度条
.progressBar(progressBar)
//还有一个对话
.progressDialog(progressDialog)
//也可以使用自定义回调
.progress(新ProgressCallback(){@Override
public void onProgress(长时间下载,长时间总计){
系统输出打印项次(“+下载+”/“+总计);
}
})
.write(新文件(“/sdcard/really big File.zip”))
.setCallback(新的FutureCallback(){
@凌驾
未完成公共作废(例外情况e,文件){
//下载完成。。。
//处理文件或错误
}
});

不要忘记添加所需的权限。希望这有帮助:)

点击下载后使用下面的代码

  File sdCard = Environment.getExternalStorageDirectory();
                File dir = new File (sdCard.getAbsolutePath() + "/myCustomFolderName");
                dir.mkdirs();
                File file = new File(dir, "you_garrit.mp4");
                Uri uri = Uri.fromFile(file);

                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadURL));
                request.setTitle("You garrit");
                request.setDescription("DownloadProgress sample");
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationUri(uri);
                request.allowScanningByMediaScanner();

                downloadID = downloadManager.enqueue(request);
                downloadProgressView.show(downloadID, new DownloadProgressView.DownloadStatusListener() {
                    @Override
                    public void downloadFailed(int reason) {
                        Log.d(TAG, "downloadFailed" + reason);
                    }

                    @Override
                    public void downloadSuccessful() {
                        Log.d(TAG, "downloadSuccessful");
                    }

                    @Override
                    public void downloadCancelled() {
                      Log.d(TAG, "downloadCancelled");
                    }
                });
                downloadButton.setVisibility(View.GONE);

如果这些不适合您,请让我知道

单击下载后使用下面的代码

  File sdCard = Environment.getExternalStorageDirectory();
                File dir = new File (sdCard.getAbsolutePath() + "/myCustomFolderName");
                dir.mkdirs();
                File file = new File(dir, "you_garrit.mp4");
                Uri uri = Uri.fromFile(file);

                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadURL));
                request.setTitle("You garrit");
                request.setDescription("DownloadProgress sample");
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationUri(uri);
                request.allowScanningByMediaScanner();

                downloadID = downloadManager.enqueue(request);
                downloadProgressView.show(downloadID, new DownloadProgressView.DownloadStatusListener() {
                    @Override
                    public void downloadFailed(int reason) {
                        Log.d(TAG, "downloadFailed" + reason);
                    }

                    @Override
                    public void downloadSuccessful() {
                        Log.d(TAG, "downloadSuccessful");
                    }

                    @Override
                    public void downloadCancelled() {
                      Log.d(TAG, "downloadCancelled");
                    }
                });
                downloadButton.setVisibility(View.GONE);


如果这些不适用于您,请告诉我

如果我正确获取您的信息,您希望将文件保存在sd卡中的myCustomFolder文件夹中,但您无法在sd卡中看到该文件夹card@jiteshmohite,我怎样才能解决这个问题?请帮助我,我亲爱的朋友好的,你的电脑里有sd卡吗device@jiteshmohite是的,我的朋友。我可以用以下代码在sd卡中创建文件夹:
final File myDirectory=new File(Environment.getExternalStorageDirectory(),“dirName”);如果(!myDirectory.exists()){myDirectory.mkdirs();}
但我应该创建文件夹并用以下代码保存:
request.setdestinationnexternalfilesdir(getApplicationContext(),“myCustomFolderName”,“p30download.com.apk”)在此库中!使用此代码时不要创建文件夹,也不要将文件保存在此文件夹中@吉特什莫希特,你在吗?你们能帮我吗?若我没弄错,你们想把文件保存在sd卡的myCustomFolder文件夹中,但你们在sd卡中看不到那个文件夹card@jiteshmohite,我怎样才能解决这个问题?请帮助我,我亲爱的朋友好的,你的电脑里有sd卡吗device@jiteshmohite是的,我的朋友。我可以用以下代码在sd卡中创建文件夹:
final File myDirectory=new File(Environment.getExternalStorageDirectory(),“dirName”);如果(!myDirectory.exists()){myDirectory.mkdirs();}
但我应该创建文件夹并用以下代码保存:
request.setdestinationnexternalfilesdir(getApplicationContext(),“myCustomFolderName”,“p30download.com.apk”)在此库中!使用此代码时不要创建文件夹,也不要将文件保存在此文件夹中@吉特什莫希特,你在吗?你能帮我吗?你能把我上面的代码全部寄给我吗?请将我的上述代码和完整代码一起发送给我好吗?请给我打电话。但在ion中,通知中不显示下载,服务中不显示下载!使用此库可创建通知I。但在ion中,通知中不显示下载,服务中不显示下载!使用此库创建通知WOWW!!!!你太棒了,Thaaaaaaaaaaanks,我亲爱的朋友。谢谢!谢谢!谢谢!!!!你太棒了,Thaaaaaaaaaaanks,我亲爱的朋友。谢谢谢谢