2012-10-29 90 views
0

我使用的相機意圖打開相機相機扔cameraIntent拍攝照片後,照片的路徑:安卓:獲取

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 

現在我試圖讓所拍攝的照片的路徑,但它的投擲的錯誤:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
if (requestCode == CAMERA_REQUEST) { 
Uri uri = data.getData(); 
String imagePath = getRealPathFromURI(uri); 
} 
} 

private String getRealPathFromURI(Uri contentUri) { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
// import android.support.v4.content.CursorLoader; i import this for CursorLoader 
    CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null); 
    Cursor cursor = loader.loadInBackground(); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 

它投擲的錯誤是:

FATAL EXCEPTION: main 
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100,  result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.xxx.xxx.BbmpActivity}: java.lang.NullPointerException 

at android.app.ActivityThread.deliverResults(ActivityThread.java:2536) 
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578) 
at android.app.ActivityThread.access$2000(ActivityThread.java:117) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:130) 
at android.app.ActivityThread.main(ActivityThread.java:3687)  
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)  
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
at android.content.ContentResolver.acquireProvider(ContentResolver.java:743)  
at android.content.ContentResolver.query(ContentResolver.java:256) 
at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49) 
at com.pixel.bbmp.BbmpActivity.getRealPathFromURI2(BbmpActivity.java:546) 
at com.pixel.bbmp.BbmpActivity.onActivityResult(BbmpActivity.java:491) 
at android.app.Activity.dispatchActivityResult(Activity.java:3908) 
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 

我想是因爲這條線就拋出錯誤:

CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null); 

這裏我需要給getApplicationContext(代替) 我也試試這個,但直到它引發錯誤:

CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null); 

當我開始這個應用程序則拋出一個錯誤是:

Could not find class 'android.content.CursorLoader', referenced from method com.xxx.xxx.BbmpActivity.getRealPathFromURI 

回答

1

而不是Uri uri = data.getData();寫如..

if(data!=null){ 
    if (requestCode == CAMERA_REQUEST) { 
      Uri uri = data.getExtras().get("data"); 
     //or... 
    Bitmap bitmapPicture = (Bitmap) data.getExtras().get("data"); 
    // other code 
} 
} 

它會正常工作。最主要的是data.getExtras().get("data");

+0

其顯示錯誤:類型不匹配:不能從Object轉換爲Uri for uri uri = data.getExtras()。get(「data」);如果我寫Bitmap bitmapPicture =(Bitmap)data.getExtras()。get(「data」);那麼我如何發送uri作爲參數getRealPathFromURI方法 –

+0

你可以壓縮位圖,然後以字節的形式發送到流 – mainu

+0

我的問題是我如何獲得路徑。 Uri uri = data.getExtras()。get(「data」);這一行拋出錯誤。這是類型不匹配:無法從對象轉換爲Uri –

0
CursorLoader loader = new CursorLoader(getApplicationContext(), 
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
proj, null, null, null); 
+0

@HowardPautz:你有一個想法,爲什麼原來的問題被刪除? –

+0

對不起,我不記得那些已經死了的鏈接是怎麼說的 - 正在清理。我遇到過這個問題,因爲三星設備沒有讀取/ storage/sdcard0中的文件... –

0

您可以指定一個URI之前,當設置拍攝意圖:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(this.mCameraFile)); 
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

哪裏mCameraFile是我創建了一個文件:

this.mCameraFile = File.createTempFile("toto", "jpg", fAlbum); 

其中fAlbum,是代表我的相冊文件夾的文件:

this.fAlbum = Environment.getExternalStorageDirectory() + "/dcim/" + "tata";