当我执行此代码时,它在我的模拟器中工作正常(甚至保存图像),但它会关闭手机上的应用程序(nexus 5)
当我在Uri行周围设置try catch时,程序不会崩溃(但是它的null值也可能对它下面的代码没有影响)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== 0 && resultCode == Activity.RESULT_OK) {
Bitmap x = (Bitmap) data.getExtras().get("data");
userImage.setImageBitmap(x);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "profileimage");
values.put(Images.Media.BUCKET_ID, "profileimage");
values.put(Images.Media.DESCRIPTION, "profile Image");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
x.compress(Bitmap.CompressFormat.JPEG, 70, outstream);
outstream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
}
干杯!
编辑: (抱歉我的不活动 - 记录的新Android手机:D)
这是错误日志:
03-28 15:33:51.230: E/Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/ext ActivityFilt[ 03-28 15:33:51.231 10005:10005 D/AndroidRuntime(10005): Shutting down VM
03-28 15:33:51.238: E/AndroidRuntime(10005): FATAL EXCEPTION: main
03-28 15:33:51.238: E/AndroidRuntime(10005): Process: com.td.tdabslidingmenu, PID: 10005
03-28 15:33:51.238: E/AndroidRuntime(10005): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.td.tdabslidingmenu/com.td.tdabslidingmenu.MainActivity}: java.lang.NullPointerException: uri
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.-wrap16(ActivityThread.java)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.os.Looper.loop(Looper.java:148)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.main(ActivityThread.java:5417)
03-28 15:33:51.238: E/AndroidRuntime(10005): at java.lang.reflect.Method.invoke(Native Method)
03-28 15:33:51.238: E/AndroidRuntime(10005): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-28 15:33:51.238: E/AndroidRuntime(10005): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-28 15:33:51.238: E/AndroidRuntime(10005): Caused by: java.lang.NullPointerException: uri
03-28 15:33:51.238: E/AndroidRuntime(10005): at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:922)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.content.ContentResolver.openOutputStream(ContentResolver.java:702)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.content.ContentResolver.openOutputStream(ContentResolver.java:678)
03-28 15:33:51.238: E/AndroidRuntime(10005): at com.td.tdabslidingmenu.MainActivity.onActivityResult(MainActivity.java:263)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.Activity.dispatchActivityResult(Activity.java:6456)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
03-28 15:33:51.238: E/AndroidRuntime(10005): ... 9 more
答案 0 :(得分:0)
您的Nexus 5运行的是哪个Android版本?如果它是Marshmallow(或N预览版),则很可能是由代码的Media.EXTERNAL_CONTENT_URI
部分引起的权限问题。