我正在开发一个遵循教程的Android上的学生注册应用程序。我需要创建一个新的学生在手机上保存照片。从Android的内部存储器获取图像时出现NullPointerException
问题是我无法从检索到的URI加载图像。
我的 “脚本” 日志打印:
Img uri: content://com.android.providers.media.documents/document/image%3A30
但我imgString为空。
我正在使用genymotion模拟器。
这里是调用库的代码...
public void loadImage(View view){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, IMG_SDCARD_ACTIVITY);
}
这是处理来自画廊的答案onActivityResult ...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
File file;
String pass = null;
Log.i("Script", "RequestCode: "+requestCode+" ResultCode: "+resultCode);
if(requestCode == IMG_SDCARD_ACTIVITY && resultCode == Activity.RESULT_OK){
try{
Uri img = data.getData();
Log.i("Script", "Img uri: "+img.toString());
String[] cols = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(img, cols, null, null, null);
cursor.moveToFirst();
int indexCol = cursor.getColumnIndex(cols[0]);
String imgString = cursor.getString(indexCol);
cursor.close();
file = new File(imgString); //this is the line 108 where the problem is...
ivImg.setImageBitmap(ImageUtil.setPic(Uri.fromFile(file), 100, 100));
ivImg.setContentDescription(file.getPath());
pass = "passou";
}
catch (NullPointerException e){e.printStackTrace();}
catch (Exception e){e.printStackTrace();}
finally {
if(pass==null){
ivImg.setImageResource(R.drawable.person);
Toast.makeText(SalvarAlunoActivity.this, "Fail! Try again.", Toast.LENGTH_SHORT).show();
}
}
}
}
这是ImageUtil类。 ..
public class ImageUtil {
public static Bitmap setPic(Uri uriFile, int width, int height){
//Get the dimensions of the view
int targetW = width;
int targetH = height;
//Get the dimensions of the Bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(uriFile.getPath(), bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
//Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
//Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(uriFile.getPath(), bmOptions);
return(bitmap);
}
}
个日志打印也是这个错误...
10-28 08:19:45.602 10874-10874/? W/System.err﹕ java.lang.NullPointerException
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at java.io.File.fixSlashes(File.java:185)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at java.io.File.<init>(File.java:134)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at com.XXXX.contentProvider.SalvarAlunoActivity.onActivityResult(SalvarAlunoActivity.java:108)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at android.app.Activity.dispatchActivityResult(Activity.java:5423)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at android.app.ActivityThread.access$1300(ActivityThread.java:135)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-28 08:19:45.606 10874-10874/? W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
这里它了我的第一次。让我知道是否需要提供更多信息。
在此先感谢。
这是什么问题? resultCode = -1或requestCode = -1 – activesince93
-1的结果代码是'RESULT_OK'。请详细解释为什么这是一个问题。 – CommonsWare
其实它确实不是问题。我现在已经检查过了......事情是我无法加载图像。我会编辑帖子。 – DMS