我的应用程序在某些设备上运行正常。不幸的是,其他人报告了以下错误提交结果,我不知道如何解决
所以我使用以下代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && data!=null) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
String s = getPath(uri); //Null string on some devices
if(s!=null){
File f = new File(s);
}
else{
//????????
}
if(resultCode== RESULT_CANCELED){
}
}
}
一种叫做getPath的方法:
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
} 此外,当用户点击:
image = (ImageView) findViewById(R.id.imageView7);
i.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.setType("image/*");
intent1.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent1,"Picture"), 1);
}
});
最后我从logcat获得了什么
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content: flg=0x1 }} to activity {****************.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3942)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
at android.app.ActivityThread.access$1300(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.io.File.fixSlashes(File.java:185)
at java.io.File.<init>(File.java:134)
at *************.MainActivity.onActivityResult(Unknown Source)
at android.app.Activity.dispatchActivityResult(Activity.java:5535)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3938)
... 11 more
我做错了什么?
答案 0 :(得分:3)
尝试此意图
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
试试这个onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}