Java Android棉花糖和棒棒糖中的文件下载失败
我创建了一个“MP4文件下载”按钮。它在安卓派和奥利奥中运行良好,但在棉花糖或棒棒糖中不起作用。它不会下载这些版本中的文件。有人能指出我的代码中缺少了什么吗?提前谢谢 这是我的下载代码Java Android棉花糖和棒棒糖中的文件下载失败,java,android,Java,Android,我创建了一个“MP4文件下载”按钮。它在安卓派和奥利奥中运行良好,但在棉花糖或棒棒糖中不起作用。它不会下载这些版本中的文件。有人能指出我的代码中缺少了什么吗?提前谢谢 这是我的下载代码 public class DownloadFile extends AsyncTask<String, String, String> { private ProgressDialog progressDialog; private String fileName; priv
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();
}
}
它给了我这个错误-“/storage/emulated/0/Bharti News/2019-05-22-02-19-09_dWJZ7AQesz48Ol2o.mp4:open failed:enoint(没有这样的文件或目录)”您应该检查用户是否通过使用以下命令授予了外部存储的权限:
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);}
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);}