gpt4 book ai didi

java - Android Marshmallow 和 Lollipop 中的文件下载失败

转载 作者:行者123 更新时间:2023-12-02 09:52:09 24 4
gpt4 key购买 nike

我创建了一个“MP4 文件下载”按钮。它在 android Android Pie 和 Oreo 中工作正常,但在 Marshmallow 或 Lollipop 中工作不正常。它不会下载这些版本中的文件。有人可以指出我的代码中缺少什么吗?提前致谢。

这是我的下载代码

public class DownloadFile extends AsyncTask<String, String, String> {

private ProgressDialog progressDialog;
private String fileName;
private String folder;
private boolean isDownloaded;

/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
this.progressDialog = new ProgressDialog(context);
this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.progressDialog.setCancelable(false);
this.progressDialog.show();
}

/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// getting file length
int lengthOfFile = connection.getContentLength();


// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);

String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());

//Extract file name from URL
fileName = f_url[0].substring(f_url[0].lastIndexOf('/') + 1, f_url[0].length());

//Append timestamp to file name
fileName = timestamp + "_" + fileName;

//External directory path to save file
folder = Environment.getExternalStorageDirectory() + File.separator + "Bharti News/";

//Create androiddeft folder if it does not exist
File directory = new File(folder);

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

// Output stream to write file
OutputStream output = new FileOutputStream(folder + fileName);

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lengthOfFile));
Log.d(TAG, "Progress: " + (int) ((total * 100) / lengthOfFile));

// writing data to file
output.write(data, 0, count);
}

// flushing output
output.flush();

// closing streams
output.close();
input.close();
return "Downloaded at: " + folder + fileName;

} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}

return "Something went wrong";
}

/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
progressDialog.setProgress(Integer.parseInt(progress[0]));
}


@Override
protected void onPostExecute(String message) {
// dismiss the dialog after the file was downloaded
this.progressDialog.dismiss();

// Display File path after downloading
Toast.makeText(context,
message, Toast.LENGTH_LONG).show();
}
}

我就是这么调用它的。

new DownloadFile().execute(item.getBodyurl());

它给了我这个错误 - '/storage/emulated/0/Bharti News/2019-05-22-02-19-09_dWJZ7AQesz48Ol2o.mp4:打开失败:ENOENT(没有这样的文件或目录)'

最佳答案

您应该使用以下方法检查用户是否已授予外部存储权限:

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
new DownloadFile().execute(item.getBodyurl());
return true;
}else{ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);}

关于java - Android Marshmallow 和 Lollipop 中的文件下载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56246576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com