尝试创建有助于下载文件的应用。我单击下载按钮,然后出现进度栏。它显示进度。然后进度条结束,我收到ENOENT错误:
E/Error:: /storage/emulated/0/logo.png: open failed: EACCES (Permission denied)
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/logo.png: open failed: ENOENT (No such file or directory)
无法理解此代码的问题,我在模拟器和真实设备上对其进行了测试。
public class DownloadProgress extends AppCompatActivity {
Button btnShowProgress;
private ProgressDialog pDialog;
ImageView my_image;
public static final int progress_bar_type = 0;
private static String file_url = "http://idsp.ak.gov.ng/images/logo.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
my_image = (ImageView) findViewById(R.id.my_image);
btnShowProgress.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
new DownloadFileFromURL().execute(file_url);
}
});
}
@Override
protected Dialog onCreateDialog(int id){
switch (id){
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute(){
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url){
int count;
try{
URL url = new URL(f_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "/logo.png";
File imageFile = new File(storageDir+fileName);
OutputStream output = new FileOutputStream(imageFile);
byte data[] = new byte[1024];
long total = 0;
while((count = input.read(data)) != -1){
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}catch (Exception e){
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress){
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url){
dismissDialog(progress_bar_type);
String imagePath = Environment.getExternalStorageDirectory() + "/logo.png";
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
}
感谢您阅读这篇文章!
答案 0 :(得分:0)
根据您的问题:
/storage/emulated/0/logo.png: open failed: EACCES (Permission denied)
是您遇到问题的主要原因。
从Android 6.0(API级别23)开始,您不仅应该声明
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
{p> AndroidManifest.xml
中的权限,以及request it in run time中的权限:
如果您的应用需要危险许可,则必须检查您是否 每次执行需要执行的操作时都要获得该许可 该许可。从Android 6.0(API级别23)开始,用户可以 随时撤消任何应用程序的权限,即使该应用程序定位到 较低的API级别。因此,即使该应用昨天使用了相机, 不能认为它今天仍然具有该权限。
看看DownloadManager
-这是处理长时间运行的HTTP下载的系统服务。