2013-07-04 78 views
6

在我的應用程序中有從相機捕捉圖像的選項。但從相機獲取圖像存在問題。當我使用這個ACTION_IMAGE_CAPTURE它返回null data.Please幫我 從相機意圖獲取圖像路徑從相機意圖獲取圖像路徑

錯誤日誌:

07-04 11:22:36.902: E/AndroidRuntime(8329): FATAL EXCEPTION: main 
07-04 11:22:36.902: E/AndroidRuntime(8329): java.lang.RuntimeException: Unable to resume activity {com.pausefablogin/com.pausefablogin.AddPOI}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=111, result=-1, data=null} to activity {com.pausefablogin/com.pausefablogin.AddPOI}: java.lang.NullPointerException 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2836) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread.access$1600(ActivityThread.java:117) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.os.Looper.loop(Looper.java:130) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at java.lang.reflect.Method.invoke(Method.java:507) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at dalvik.system.NativeStart.main(Native Method) 
07-04 11:22:36.902: E/AndroidRuntime(8329): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=111, result=-1, data=null} to activity {com.pausefablogin/com.pausefablogin.AddPOI}: java.lang.NullPointerException 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2536) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  ... 13 more 
07-04 11:22:36.902: E/AndroidRuntime(8329): Caused by: java.lang.NullPointerException 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at com.pausefablogin.AddPOI.onActivityResult(AddPOI.java:281) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.Activity.dispatchActivityResult(Activity.java:3908) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 
07-04 11:22:36.902: E/AndroidRuntime(8329):  ... 14 more 

在此先感謝

+0

http://stackoverflow.com/questions/17426178/tell-the-camera-intent-to-save-the-taken-image/17426328#17426328。 – Nirmal

回答

3

如果成功實現的代碼,你會得到手動捕獲的圖像可以保存以供將來使用。

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      videoView.setVisibility(View.INVISIBLE); 
      imageView.setVisibility(View.VISIBLE); 
      imageView.setImageBitmap(photo); 
     } 
    } 
9

試試這個

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(i , 0);   

並稱之爲

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
     Intent resultData) { 
    super.onActivityResult(requestCode, resultCode, resultData); 

     if (resultData != null) { 

     String[] projection = { MediaStore.Images.Media.DATA }; 
       Cursor cursor = managedQuery(
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
         projection, null, null, null); 
       int column_index_data = cursor 
         .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToLast(); 

       String imagePath = cursor.getString(column_index_data); 
       Bitmap bitmapImage = BitmapFactory.decodeFile(imagePath); 
       imageView.setImageBitmap(bitmapImage); 

      } 

,並使用prrmission你想

<uses-permission android:name="android.permission.CAMERA" /> 
    <permission android:name="android.permission.FLASHLIGHT" /> 
    <uses-feature android:name="android.hardware.camera"/> 
3

您可以使用以下步驟:

// create a global variable 
File destination; 
openCameraBtn.setOnClickListener(new OnClickListener() 
{ 

      @Override 
      public void onClick(View v) { 
       destination = new File(Environment.getExternalStorageDirectory(),"image.jpg"); 

       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination)); 
       startActivityForResult(intent, CAMERA_PICTURE); 

      } 
     }); 


@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 


    if(requestCode == CAMERA_PICTURE && resultCode == Activity.RESULT_OK) 
      { 
      try{ 
      FileInputStream in = new FileInputStream(destination); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 10; //Downsample 10x 
      Log.d("PP", " bitmap factory=========="+options); 
      Bitmap user_picture_bmp = BitmapFactory.decodeStream(in, null, options); 
     userImage.setImageBitmap(user_picture_bmp); 
     } catch (Exception e) 
      { e.printStackTrace(); 
     } 

    } 

請聲明許可和功能在您的清單文件。

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 

如果u想更詳細,那麼你可以用下面的鏈接:

http://developer.android.com/guide/topics/media/camera.html

我希望你能成功。

+0

我必須添加在CAMERA_PICTURE中。我可以添加任何數量或特定的東西 –

+0

您在清單文件中給予權限。 – Yogendra

+0

此代碼在 中發出異常destination = new File(Environment.getExternalStorageDirectory(),「image.jpg」); –

1
Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC"); 
     if(cursor != null && cursor.moveToFirst()) 
     { 
      do { 
       Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA))); 
       String photoPath = uri.toString(); 
      }while(cursor.moveToNext()); 


      cursor.close(); 
     } 

當while循環做最後一次時,它給出了最後一次捕獲的圖像路徑。

5

我有同樣的問題,以下爲我工作:

@Override 
public void onClick(View v) { 
     Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(Intent.createChooser(intent, "Select Picture"), CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
       } 
     }); 
    } 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
//super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) { 
     final ContentResolver cr = getContentResolver(); 
     final String[] p1 = new String[] { 
      MediaStore.Images.ImageColumns._ID, 
      MediaStore.Images.ImageColumns.DATE_TAKEN 
       }; 
    Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC"); 
     if (c1.moveToFirst()) { 
     String uristringpic = "content://media/external/images/media/" +c1.getInt(0); 
     Uri uri = Uri.parse(uristringpic); 
      try { 
       Bitmap bm = android.provider.MediaStore.Images.Media.getBitmap(cr, uri); 

希望這有助於我所知,這是多麼令人沮喪。

2

您可能正在運行三星設備。我相信這是一個在這些設備中發生的錯誤。否則您必須獲取URI。這樣做:

/** 
    * (This is a variable instance) Contains the path and file name where you want to save the image. 
    * Mainly used to start Intent.Action_View with this URI. (GalleryApp) 
    */ 
private Uri uriImage= null; 

public void onClickCamera(View v){ 
    // Cria uma intent para capturar uma imagem e retorna o controle para quem o chamou (NAO PRECISA DECLARAR PERMISSAO NO MANIFESTO PARA ACESSAR A CAMERA POIS O FAZEMOS VIA INTENT). 
    // Creates an intent to capture an image and returns control to the caller 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    // Cria um arquivo para salvar a imagem. 
    // Creates an file to save the image. 
    uriImage = ProcessaImagens.getOutputMediaFileUri(ProcessaImagens.MEDIA_TYPE_IMAGE, getActivity().getApplicationContext()); 
    // Intent to pass a URI object containing the path and file name where you want to save the image. We'll get through the data parameter of the method onActivityResult(). 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImagem); 
    // Starts the intent to image capture and waits the result. 
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
} 

ProcessaImagens類是我編寫,我與大家分享的一類。可以使用這個類來方便。如果要將圖像保存到數據庫中,它有一個非常好的方法來壓縮圖像。在其onActivityResult()方法做到這一點:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // If finished activity on startForActivityResult. 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
      String imagePath = uriImage.getPath(); 
      // I will compress the imagem. Read the javadoc from method and you'll see which it returns both bitmap and array bytes. 
      List<Object> imageCompact = ProcessaImagens.compactarImagem(uriImagem.getPath()); 
      Bitmap imageBitmap = (Bitmap) imageCompact.get(0); 
      byte[] imageBytes = (byte[]) imageCompact.get(1); 

     } 
    } 
    // If canceled activity on startForActivityResult. 
    else if (resultCode == Activity.RESULT_CANCELED) { 
     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
      // User canceled the image capture. 
      Log.d(getTag(), "Image capture canceled!"); 
     } 
    } 
    // If an error occurred in the activity on startForActivityResult. 
    else { 
     // Failed the image capture, alert the user. 
     Toast.makeText(getActivity().getApplicationContext(), "FAILED! The image capture failed!", Toast.LENGTH_LONG).show(); 
     Log.e(getTag(), "FAILED! The image capture failed!"); 
    } 
} 

請注意,我用getActivity().getApplicationContext(),因爲我碰到一個片段的背景下,而不是從活動。我相信用這種方法你可以擁有你想要的東西。只是做一點改變如何形成上下文。

+1

這是最好你翻譯和添加答案在這裏,只要鏈接只答案不被接受在這裏 – Math

+0

好吧,我會做到這一點 –

+0

@Math我編輯了我的答案。 –