问题描述:设置桌面壁纸,出现不响应anr(“com,android.wallpapercrppper发生anr)
问题分析:
1.复现问题并抓取log,分析log:
12-29 20:01:18.848 E/AndroidRuntime( 2646): FATAL EXCEPTION: AsyncTask #6
12-29 20:01:18.848 E/AndroidRuntime( 2646): Process: com.android.wallpapercropper, PID: 2646
12-29 20:01:18.848 E/AndroidRuntime( 2646): java.lang.RuntimeException: An error occurred while executing doInBackground()
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.os.AsyncTask$3.done(AsyncTask.java:325)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at java.lang.Thread.run(Thread.java:761)
12-29 20:01:18.848 E/AndroidRuntime( 2646): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/47 from pid=2646, uid=10021 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.os.Parcel.readException(Parcel.java:1692)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:693)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1147)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:984)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.content.ContentResolver.openInputStream(ContentResolver.java:704)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at com.android.wallpapercropper.WallpaperCropActivity$BitmapCropTask.regenerateInputStream(WallpaperCropActivity.java:537)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at com.android.wallpapercropper.WallpaperCropActivity$BitmapCropTask.getImageBounds(WallpaperCropActivity.java:553)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at com.android.wallpapercropper.WallpaperCropActivity$BitmapCropTask.cropBitmap(WallpaperCropActivity.java:601)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at com.android.wallpapercropper.WallpaperCropActivity$BitmapCropTask.doInBackground(WallpaperCropActivity.java:802)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at com.android.wallpapercropper.WallpaperCropActivity$BitmapCropTask.doInBackground(WallpaperCropActivity.java:801)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at android.os.AsyncTask$2.call(AsyncTask.java:305)
12-29 20:01:18.848 E/AndroidRuntime( 2646): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-29 20:01:18.848 E/AndroidRuntime( 2646): ... 4 more
12-29 20:01:18.856 I/am_crash( 515): [2646,0,com.android.wallpapercropper,949534277,java.lang.SecurityException,Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/47 from pid=2646, uid=10021 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission(),Parcel.java,1692]
12-29 20:01:18.950 I/OpenGLRenderer( 515): Initialized EGL, version 1.4
发现是com,android.wallpapercrppper缺少权限 android.permission.READ_EXTERNAL_STORAGE,
解决方案:
添加权限到 /frameworks/base/packages/WallpaperCropper/AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
添加后仍然报一样的错
再通过修改code添加权限
/frameworks/base/packages/WallpaperCropper/src/com/android/wallpapercropper/WallpaperCropActivity.java
import android.support.v4.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
if (!enableRotation()) {
setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT);
}
verifyStoragePermissions(this);
}
public static void verifyStoragePermissions(Activity activity) {
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(activity,"android.permission.READ_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有读的权限,去申请读的权限,会弹出对话框
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
pro
总结后续措施:
应用应该测完所有功能 需要什么权限提前配置好