我的应用程序必须制作一张图片,并在 imageview 中显示它。我得到了这个工作,但出乎意料的是它不再。
这是我的按钮代码:
public void makeFoto(View v) {
Calendar cal = Calendar.getInstance();
File file = new File(Environment.getExternalStorageDirectory(), (subject.getText().toString() + cal.getTimeInMillis()+".jpg"));
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
capturedImageUri = Uri.fromFile(file);
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
startActivityForResult(photoIntent, CAMERA_PHOTO_REQUEST);
}
这是在我的 onActivityResult 中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
switch(requestCode) {
case CAMERA_PHOTO_REQUEST:
Uri img = capturedImageUri;
imv.setImageURI(img);
case CHOOSE_PHOTO_REQUEST:
try {
Uri selectedImage = data.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
imv.setImageBitmap(yourSelectedImage);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如您所见,我有两个结果,一个用于拍照,另一个用于从图库中选择图片。这是我得到的错误:
04-03 18:16:29.207: E/AndroidRuntime(1478): FATAL EXCEPTION: main
04-03 18:16:29.207: E/AndroidRuntime(1478): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.icomplain/com.example.icomplain.Home}: java.lang.NullPointerException
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3019)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.access$1100(ActivityThread.java:122)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1176)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.os.Looper.loop(Looper.java:137)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-03 18:16:29.207: E/AndroidRuntime(1478): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 18:16:29.207: E/AndroidRuntime(1478): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 18:16:29.207: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-03 18:16:29.207: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-03 18:16:29.207: E/AndroidRuntime(1478): at dalvik.system.NativeStart.main(Native Method)
04-03 18:16:29.207: E/AndroidRuntime(1478): Caused by: java.lang.NullPointerException
04-03 18:16:29.207: E/AndroidRuntime(1478): at com.example.icomplain.Home.onActivityResult(Home.java:154)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
04-03 18:16:29.207: E/AndroidRuntime(1478): at android.app.ActivityThread.deliverResults(ActivityThread.java:2972)
04-03 18:16:29.207: E/AndroidRuntime(1478): ... 11 more
04-03 18:16:31.478: I/Process(1478): Sending signal. PID: 1478 SIG: 9
04-03 18:17:36.507: D/dalvikvm(1539): GC_FOR_ALLOC freed 47K, 2% free 14131K/14339K, paused 42ms
04-03 18:17:36.507: I/dalvikvm-heap(1539): Grow heap (frag case) to 14.868MB for 1048592-byte allocation
04-03 18:17:36.597: D/dalvikvm(1539): GC_CONCURRENT freed 1K, 2% free 15154K/15431K, paused 4ms+3ms
04-03 18:17:36.667: D/dalvikvm(1539): GC_FOR_ALLOC freed 0K, 2% free 15154K/15431K, paused 26ms
04-03 18:17:36.677: I/dalvikvm-heap(1539): Grow heap (frag case) to 16.646MB for 1865972-byte allocation
04-03 18:17:36.727: D/dalvikvm(1539): GC_CONCURRENT freed 0K, 2% free 16976K/17287K, paused 4ms+3ms
来自日志的图像可以在这里查看。