我真的很沮喪解決我的問題,我有一個應用程序,使用相機,當相機捕獲一張照片,它將顯示在活動上,當我不使用時cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
照片將顯示在活動上,但名稱和路徑文件不像我想要的那樣。我使用的Galaxy Tab編譯它,我必須跟着教程來解決我的問題,從here,here和here但我的應用程序仍然強制關閉和eror,這是我的代碼:安卓相機:失敗的結果ResultInfo {誰=空,請求= 0,結果= -1,數據=空}到活動
{
File sdCard= Environment.getExternalStorageDirectory();
File path = new File (sdCard.getAbsolutePath() + "/android/data/spaj_foto");
path.mkdir();
File file= new File (path,"spaj_foto.png");
String filePath ="/android/data/spaj_foto/spaj_foto.png";
try {
file.createNewFile();
} catch (IOException e) {}
mPhotoUri = Uri.fromFile(file);
values.put(MediaStore.Images.ImageColumns.DATA, file.getPath());
mPhotoUri=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
startActivityForResult(cameraIntent,TAKE_PHOTO_CODE);
}
activityresult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
image_spaj.setImageBitmap(photo);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
// System.out.println(mImageCaptureUri);
}
}
public String getRealPathFromURI(Uri mPhotoUri) {
Cursor cursor = getContentResolver().query(mPhotoUri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
這是我的logcat說:
09-27 10:59:43.186: E/AndroidRuntime(31318): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {org.example.touch/org.example.touch.FormSpaj}: java.lang.NullPointerException
09-27 10:59:43.186: E/AndroidRuntime(31318): at android.app.ActivityThread.deliverResults(ActivityThread.java:3179)
09-27 10:59:43.186: E/AndroidRuntime(31318): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222)
09-27 10:59:43.186: E/AndroidRuntime(31318): at android.app.ActivityThread.access$1100(ActivityThread.java:140)
09-27 10:59:43.186: E/AndroidRuntime(31318): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
09-27 10:59:43.186: E/AndroidRuntime(31318): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 10:59:43.186: E/AndroidRuntime(31318): at android.os.Looper.loop(Looper.java:137)
09-27 10:59:43.186: E/AndroidRuntime(31318): at android.app.ActivityThread.main(ActivityThread.java:4895)
09-27 10:59:43.186: E/AndroidRuntime(31318): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 10:59:43.186: E/AndroidRuntime(31318): at java.lang.reflect.Method.invoke(Method.java:511)
09-27 10:59:43.186: E/AndroidRuntime(31318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
09-27 10:59:43.186: E/AndroidRuntime(31318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
09-27 10:59:43.186: E/AndroidRuntime(31318): at dalvik.system.NativeStart.main(Native Method)
09-27 10:59:43.186: E/AndroidRuntime(31318): Caused by: java.lang.NullPointerException
09-27 10:59:43.186: E/AndroidRuntime(31318): at org.example.touch.FormSpaj.onActivityResult(FormSpaj.java:993)
09-27 10:59:43.186: E/AndroidRuntime(31318): at android.app.Activity.dispatchActivityResult(Activity.java:5347)
09-27 10:59:43.186: E/AndroidRuntime(31318): at android.app.ActivityThread.deliverResults(ActivityThread.java:3175)
09-27 10:59:43.186: E/AndroidRuntime(31318): ... 11 more
我真的希望有人能幫助我解決我的問題,我已經ş大頭貼,不能再反思了,請別人幫我......謝謝。
這只是一個空指針異常,FormSpaj.java:993是什麼? –
位圖photo =(位圖)data.getExtras()。get(「data」); –
'getExtras()'可以返回null,如果沒有額外的Bundle。你應該確保'getExtras()'返回的值不是'null'。 – jedwards