我正在尝试启动相机并在不活动结果中捕获图像文件,然后将其保存在文件中..但我在getContentResolver.notifyChange行继续获取java.lang.NullPointerException。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 1001:
if(resultCode == Activity.RESULT_OK) {
getContentResolver().notifyChange(imageUri, null);
imageView = (ImageView )findViewById(R.id.take_photo_image_view);
ContentResolver contentResolver = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(contentResolver,
imageUri);
imageView.setImageBitmap(bitmap);
} catch(Exception e) {
Toast.makeText(GetPhotoActivity.this, "failed to load",
Toast.LENGTH_LONG).show();
Log.e(LOG_TAG, e.toString());
}
}
}
}
11-23 17:26:00.508 32727-32727/com.example.testspotter E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testspotter, PID: 32727
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=null} to activity {com.example.testspotter/com.example.testspotter.GetPhotoActivity}: 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:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1480)
at android.os.Parcel.readException(Parcel.java:1428)
at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:486)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1668)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1657)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1637)
at com.example.testspotter.GetPhotoActivity.onActivityResult(GetPhotoActivity.java:126)
答案 0 :(得分:0)
使用以下代码调用相机活动:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, REQUEST_CAMERA);
在onActivityResult
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
File f = new File(Environment.getExternalStorageDirectory()
.toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bm;
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
imageView = (ImageView )findViewById(R.id.take_photo_image_view);
bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
btmapOptions);
// bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
imageView.setImageBitmap(bm);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream fOut = null;
File file = new File(path, String.valueOf(System
.currentTimeMillis()) + ".jpg");
try {
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}