使用相机捕获图像 - 无法提供结果 ResultInfo
capture image with camera - Failure delivering result ResultInfo
我想用相机捕捉图像,我是这样做的:
启动相机:
public void startPhotoTaker() {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "cs_" + new Date().getTime() + ".jpg");
mLastPhoto = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mLastPhoto);
// start the image capture Intent
startActivityForResult(intent, REQUEST_TAKE_PICTURE);
}
onActivityResult :
if (requestCode == REQUEST_TAKE_PICTURE) {
/**
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mLastPhoto);
this.sendBroadcast(mediaScanIntent);
*/
File file = new File(getRealPathFromURI(mLastPhoto));
final Handler handler = new Handler();
MediaScannerConnection.scanFile(
this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, final Uri uri) {
handler.post(new Runnable() {
@Override
public void run() {
handleSendDelete(mLastPhoto, "image/*", true);
}
});
}
});
}
问题是当我回到应用程序时,它出现错误!
它的应用 logcat :
03-03 13:02:22.851: ERROR/AndroidRuntime(1808): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {info.guardianproject.otr.app.im/info.guardianproject.otr.app.im.app.ChatViewActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=null} to activity {info.guardianproject.otr.app.im/info.guardianproject.otr.app.im.app.ChatViewActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
at android.app.ActivityThread.access0(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=null} to activity {info.guardianproject.otr.app.im/info.guardianproject.otr.app.im.app.ChatViewActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2431)
... 12 more
Caused by: java.lang.NullPointerException
at info.guardianproject.otr.app.im.app.ChatViewActivity.getRealPathFromURI(ChatViewActivity.java:424)
at info.guardianproject.otr.app.im.app.ChatViewActivity.onActivityResult(ChatViewActivity.java:336)
at android.app.Activity.dispatchActivityResult(Activity.java:4649)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
... 13 more
我调试我的应用程序,我认为这个字段:mLastPhoto 为空!但我不知道为什么?当我启动相机时它很有价值!
更新:
public String getRealPathFromURI(Uri contentUri) {
try {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return contentUri.getPath();
}
}
尝试将您的 getRealPathFromURI()
方法替换如下,可能是因为已弃用 ManagedQuery()
,另外请告诉我们您使用的设备有足够的 space?
public String getRealPathFromURI(Uri contentUri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if(cursor.moveToFirst()){;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
同时更新您的 onActivityResult,方法是更新以下两行
if(resultCode == RESULT_OK) {
if (requestCode == REQUEST_TAKE_PICTURE)
{
// Put your code here/.....
}
}
我用这种方式解决了我的问题(我不确定这是最好的方法,但对我有用):
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("mLastPhoto", String.valueOf(mLastPhoto));
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mLastPhoto = Uri.parse(savedInstanceState.getString("mLastPhoto"));
}
我想用相机捕捉图像,我是这样做的:
启动相机:
public void startPhotoTaker() {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "cs_" + new Date().getTime() + ".jpg");
mLastPhoto = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mLastPhoto);
// start the image capture Intent
startActivityForResult(intent, REQUEST_TAKE_PICTURE);
}
onActivityResult :
if (requestCode == REQUEST_TAKE_PICTURE) {
/**
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mLastPhoto);
this.sendBroadcast(mediaScanIntent);
*/
File file = new File(getRealPathFromURI(mLastPhoto));
final Handler handler = new Handler();
MediaScannerConnection.scanFile(
this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, final Uri uri) {
handler.post(new Runnable() {
@Override
public void run() {
handleSendDelete(mLastPhoto, "image/*", true);
}
});
}
});
}
问题是当我回到应用程序时,它出现错误! 它的应用 logcat :
03-03 13:02:22.851: ERROR/AndroidRuntime(1808): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {info.guardianproject.otr.app.im/info.guardianproject.otr.app.im.app.ChatViewActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=null} to activity {info.guardianproject.otr.app.im/info.guardianproject.otr.app.im.app.ChatViewActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
at android.app.ActivityThread.access0(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=null} to activity {info.guardianproject.otr.app.im/info.guardianproject.otr.app.im.app.ChatViewActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2431)
... 12 more
Caused by: java.lang.NullPointerException
at info.guardianproject.otr.app.im.app.ChatViewActivity.getRealPathFromURI(ChatViewActivity.java:424)
at info.guardianproject.otr.app.im.app.ChatViewActivity.onActivityResult(ChatViewActivity.java:336)
at android.app.Activity.dispatchActivityResult(Activity.java:4649)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
... 13 more
我调试我的应用程序,我认为这个字段:mLastPhoto 为空!但我不知道为什么?当我启动相机时它很有价值!
更新:
public String getRealPathFromURI(Uri contentUri) {
try {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return contentUri.getPath();
}
}
尝试将您的 getRealPathFromURI()
方法替换如下,可能是因为已弃用 ManagedQuery()
,另外请告诉我们您使用的设备有足够的 space?
public String getRealPathFromURI(Uri contentUri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if(cursor.moveToFirst()){;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
同时更新您的 onActivityResult,方法是更新以下两行
if(resultCode == RESULT_OK) {
if (requestCode == REQUEST_TAKE_PICTURE)
{
// Put your code here/.....
}
}
我用这种方式解决了我的问题(我不确定这是最好的方法,但对我有用):
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("mLastPhoto", String.valueOf(mLastPhoto));
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mLastPhoto = Uri.parse(savedInstanceState.getString("mLastPhoto"));
}