【发布时间】:2015-08-30 16:54:50
【问题描述】:
我的应用程序在某些设备上运行良好。不幸的是,其他人报告了以下错误,无法提供我不知道如何解决的结果
所以我使用以下代码:
@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
我做错了什么?
【问题讨论】:
-
答对后有没有留下什么东西
-
对不起我的朋友,但我不明白你的问题。非常感谢您的代码完美运行!
标签: android string file onactivityresult