动态申请权限之后还是报原来的错!为什么解决问题的评论多了就看不到评论了?
com.studio.handlerproject; android.Manifest; android.app.Activity; android.app.AlertDialog; android.content.DialogInterface; android.content.Intent; android.content.pm.PackageManager; android.net.Uri; android.os.Bundle; android.os.Environment; android.os.Handler; android.os.Message; android.provider.Settings; android.support.v4.app.ActivityCompat; android.support.v4.content.ContextCompat; android.view.View; android.widget.ProgressBar; android.widget.Toast; java.io.File; java.io.FileOutputStream; java.io.IOException; java.io.InputStream; java.io.OutputStream; java.net.MalformedURLException; java.net.URL; java.net.URLConnection; DownloadActivity Activity { = ; Handler ; =; = ; onCreate(Bundle savedInstanceState) { .onCreate(savedInstanceState); setContentView(R.layout.); ProgressBar progressBar= (ProgressBar) findViewById(R.id.); findViewById(R.id.).setOnClickListener(View.OnClickListener() { onClick(View v) { Thread(Runnable() { run() { download(); } }).start(); } }); = Handler(){ handleMessage(Message msg) { .handleMessage(msg); (msg.){ : .setProgress((Integer) msg.); number=(Integer) msg.; (number==){ Toast.(DownloadActivity., , Toast.).show(); } ; : Toast.(DownloadActivity., , Toast.).show(); ; } } }; } download(String s) { { URL url=URL(s); { URLConnection urlConnection=url.openConnection(); InputStream inputStream=urlConnection.getInputStream(); contentLength=urlConnection.getContentLength(); String downloadFolderName= Environment.()+ File.++File.; File file=File(downloadFolderName); (!file.exists()){ file.mkdirs(); } String fileName=downloadFolderName+; File apkFile=File(fileName); (apkFile.exists()){ apkFile.delete(); } downloadSize=; [] bytes=[]; length; OutputStream outputStream=FileOutputStream(fileName); ((length=inputStream.read(bytes))!=-){ outputStream.write(bytes,,length); downloadSize+=length; Message message=Message.(); message.=downloadSize*/contentLength; message.= ; .sendMessage(message); } inputStream.close(); outputStream.close(); } (IOException e) { Message message=Message.(); message.= ; .sendMessage(message); e.printStackTrace(); } } (MalformedURLException e) { Message message=Message.(); message.= ; .sendMessage(message); e.printStackTrace(); } } click(View view) { isAllGranted = checkPermissionAllGranted( String[] { Manifest.permission., Manifest.permission., Manifest.permission.} ); (isAllGranted) { doBackup(); ; } ActivityCompat.( , String[] { Manifest.permission., Manifest.permission., Manifest.permission.}, ); } checkPermissionAllGranted(String[] permissions){ (String permission : permissions){ (ContextCompat.(,permission)!=PackageManager.){ ; } } ; } onRequestPermissionsResult(requestCode, String[] permissions, [] grantResults) { .onRequestPermissionsResult(requestCode, permissions, grantResults); (requestCode==){ isAllGranted=; (grant : grantResults){ (grant!=PackageManager.){ isAllGranted=; ; } } (isAllGranted){ doBackup(); }{ openAppDetails(); } } } doBackup(){ Toast.(DownloadActivity., , Toast.).show(); } openAppDetails(){ AlertDialog.Builder builder=AlertDialog.Builder(); builder.setMessage(); builder.setPositiveButton(, DialogInterface.OnClickListener() { onClick(DialogInterface dialog, which) { Intent intent=Intent(); intent.setAction(Settings.); intent.addCategory(Intent.); intent.setData(Uri.(+getPackageName())); intent.addFlags(Intent.); intent.addFlags(Intent.); intent.addFlags(Intent.); startActivity(intent); } }); builder.setNegativeButton(,); builder.show(); } }
正在回答
你把不行的代码贴出来,我运行后方便快速帮你找到原因
public void click(View view) {
Log.d(TAG, "download: "+"进入View");
boolean isAllGranted = checkPermissionAllGranted(
new String[] {
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}
);
if (isAllGranted) {
Log.d(TAG, "download: "+"判断isAllGranted");
doBackup();
return;
}
ActivityCompat.requestPermissions(
this,
new String[] {
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
MY_PERMISSION_REQUEST_CODE
);
Log.d(TAG, "download: "+"View结束");
}
private boolean checkPermissionAllGranted(String[] permissions){
for(String permission : permissions){
if(ContextCompat.checkSelfPermission(this,permission)!=PackageManager.PERMISSION_GRANTED){
Log.d(TAG, "download: "+"检测到权限没开");
return false;
}
}
Log.d(TAG, "download: "+"检测到权限全开");
return true;
}
private void doBackup(){
Toast.makeText(DownloadActivity.this, "获得权限", Toast.LENGTH_SHORT).show();
}
报错如下:
08-22 02:19:09.358 2457-2797/com.studio.handlerproject W/System.err: java.io.FileNotFoundException: /storage/emulated/0/imooc/imooc.apk (No such file or directory)
08-22 02:19:09.358 2457-2797/com.studio.handlerproject W/System.err: at java.io.FileOutputStream.open(Native Method)
08-22 02:19:09.358 2457-2797/com.studio.handlerproject W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
08-22 02:19:09.358 2457-2797/com.studio.handlerproject W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:108)
08-22 02:19:09.358 2457-2797/com.studio.handlerproject W/System.err: at com.studio.handlerproject.DownloadActivity.download(DownloadActivity.java:106)
08-22 02:19:09.358 2457-2797/com.studio.handlerproject W/System.err: at com.studio.handlerproject.DownloadActivity.access$000(DownloadActivity.java:35)
08-22 02:19:09.358 2457-2797/com.studio.handlerproject W/System.err: at com.studio.handlerproject.DownloadActivity$1$1.run(DownloadActivity.java:54)
08-22 02:19:09.358 2457-2797/com.studio.handlerproject W/System.err: at java.lang.Thread.run(Thread.java:761)
08-22 02:19:09.427 2457-2495/com.studio.handlerproject E/EGL_emulation: tid 2495: eglSurfaceAttrib(1227): error 0x3009 (EGL_BAD_MATCH)
08-22 02:19:09.428 2457-2495/com.studio.handlerproject W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x78d67e902100, error=EGL_BAD_MATCH
08-22 02:19:09.430 2457-2495/com.studio.handlerproject D/EGL_emulation: eglMakeCurrent: 0x78d67e864dc0: ver 2 0 (tinfo 0x78d67e869100)
08-22 02:19:09.464 2457-2495/com.studio.handlerproject D/EGL_emulation: eglMakeCurrent: 0x78d67e864dc0: ver 2 0 (tinfo 0x78d67e869100)
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星