2017-08-14 103 views
0

新的android,我有一個按鈕來打開畫廊選擇一張圖片,並將其顯示到一個ImageView,它工作正常,另一個按鈕打開相機使用意圖,我可以打開相機拍攝一張圖片,但點擊保存圖片後,應用程序崩潰。拍攝的圖像保存在正確的文件夾中。當debbugin的應用程序的錯誤拋出它的線:相機停止,位圖爲空

filePath = data.getData(); 

和位圖顯示爲空。 我已經添加了權限,我已經閱讀了很多關於此的帖子,但仍然無法弄清楚問題所在。任何幫助表示讚賞。 這是代碼。

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

    if(requestCode == CAM_REQUEST && resultCode == Activity.RESULT_OK){ 
     Toast.makeText(TerminosYC.this.getActivity(), "si hay foto", Toast.LENGTH_LONG).show(); 

filePath = data.getData(); 

try { 
      bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath); 
      imageView.setImageBitmap(bitmap); 

      areaLeyendaImagen.setVisibility(View.VISIBLE); 
      areaFotoCargada.setVisibility(View.VISIBLE); 
      imagencargada.setVisibility(View.VISIBLE); 
      textoimagencargada.setVisibility(View.VISIBLE); 
      area_cerrarIagen.setVisibility(View.VISIBLE); 
      cerrarImagen.setVisibility(View.VISIBLE); 

      areaLeyendaImagen.setFocusableInTouchMode(true); 
      areaLeyendaImagen.requestFocus(); 
      areaLeyendaImagen.setFocusable(true); 
      buttonChoose.setError(null); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

}else if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) { 
     filePath = data.getData(); 
     try { 
      bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath); 
      imageView.setImageBitmap(bitmap); 

      areaLeyendaImagen.setVisibility(View.VISIBLE); 
      areaFotoCargada.setVisibility(View.VISIBLE); 
      imagencargada.setVisibility(View.VISIBLE); 
      textoimagencargada.setVisibility(View.VISIBLE); 
      area_cerrarIagen.setVisibility(View.VISIBLE); 
      cerrarImagen.setVisibility(View.VISIBLE); 
      buttonChoose.setError(null); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

public String getPath(Uri uri) { 
    Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    String document_id = cursor.getString(0); 
    document_id = document_id.substring(document_id.lastIndexOf(":") + 1); 
    cursor.close(); 

    cursor = getActivity().getContentResolver().query(
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
      null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null); 
    cursor.moveToFirst(); 
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
    cursor.close(); 

    return path; 
} 

的logcat:

a E/MotionRecognitionManager: mSContextService = null 
08-13 23:50:38.243 27745-27745/com.example.lupitagarcia.yosoyvallarta 
E/MotionRecognitionManager: motionService = 
[email protected]39e18a4 
08-13 23:50:51.483 27745-27745/com.example.lupitagarcia.yosoyvallarta 
E/AndroidRuntime: FATAL EXCEPTION: main 
                        Process: com.example.lupitagarcia.yosoyvallarta, PID: 27745 
                        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66849, result=-1, data=null} to activity {com.example.lupitagarcia.yosoyvallarta/com.example.lupitagarcia.yosoyvallarta.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference 
                         at android.app.ActivityThread.deliverResults(ActivityThread.java:5004) 
                         at android.app.ActivityThread.handleSendResult(ActivityThread.java:5047) 
                         at android.app.ActivityThread.access$1600(ActivityThread.java:229) 
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                         at android.os.Looper.loop(Looper.java:148) 
                         at android.app.ActivityThread.main(ActivityThread.java:7406) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference 
                         at com.example.lupitagarcia.yosoyvallarta.TerminosYC.onActivityResult(TerminosYC.java:463) 
                         at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165) 
                         at android.app.Activity.dispatchActivityResult(Activity.java:7165) 
                         at android.app.ActivityThread.deliverResults(ActivityThread.java:5000) 
                         at android.app.ActivityThread.handleSendResult(ActivityThread.java:5047)  
                         at android.app.ActivityThread.access$1600(ActivityThread.java:229)  
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)  
                         at android.os.Handler.dispatchMessage(Handler.java:102)  
                         at android.os.Looper.loop(Looper.java:148)  
                         at android.app.ActivityThread.main(ActivityThread.java:7406)  
                         at java.lang.reflect.Method.invoke(Native Method)  
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  

UPDATE:

一旦畫面附着在ImageView的,我點擊一個按鈕,發送圖片,將其存儲在一個數據庫,我得到這個錯誤。

FATAL EXCEPTION: main 
                        Process: com.example.lupitagarcia.yosoyvallarta, PID: 15254 
                        java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference 
                         at com.example.lupitagarcia.yosoyvallarta.TerminosYC.getPath(TerminosYC.java:549) 
                         at com.example.lupitagarcia.yosoyvallarta.TerminosYC$1.onClick(TerminosYC.java:352) 
                         at android.view.View.performClick(View.java:5721) 
                         at android.widget.TextView.performClick(TextView.java:10936) 
                         at android.view.View$PerformClick.run(View.java:22620) 
                         at android.os.Handler.handleCallback(Handler.java:739) 
                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                         at android.os.Looper.loop(Looper.java:148) 
                         at android.app.ActivityThread.main(ActivityThread.java:7406) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
+0

你在三星設備偶然測試它? – Enzokie

+0

是的,這是一個三星設備。 @Enzokie –

+0

這是三星中的一個錯誤;)因此,假設getData在某個時刻爲空。 – Enzokie

回答

0

我不知道你的代碼是做什麼的。但是,日誌說:應用程序已經崩潰,因爲空指針異常

檢查ZygoteInit.java類的main方法。在1120行,您嘗試從null實例獲取某些內容。

你應該解決這個問題。

+0

謝謝,我會檢查,並更新有關代碼的信息。 –

0

我認爲問題是在這裏,,這

bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath); 

改變這樣

Bitmap bitmap = (Bitmap) data.getExtras().get("data"); 
+0

對不起,ImagePicker會是什麼,我得到'無法解析符號' –

+0

我編輯了答案 –

+0

我已經修改它,但它一直在崩潰。同一行 –

0

的這一翻譯你可以得到位圖當REQUEST_CAMERA

Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 

你可以得到位圖當PICK_IMAGE_REQUEST from gallery

Uri uri = data.getData(); 
    try { 
     Bitmap bitmap = MediaStore.Images.Media.getBitmap(mcontext.getContentResolver(), uri); 
       // Log.d(TAG, String.valueOf(bitmap)); 
    } catch (Exception e) { 
        e.printStackTrace(); 
    } 
0

試試這個

values = new ContentValues(); 
values.put(MediaStore.Images.Media.TITLE, "New Picture"); 
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera"); 
imageUri = getContentResolver().insert(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
startActivityForResult(intent, CAMERA_CLICK_RESULT); 

onActivityResult

if (requestCode == CAMERA_CLICK_RESULT && resultCode == RESULT_OK) { 
     Bitmap photo = null; 
     try { 
      photo = MediaStore.Images.Media.getBitmap(
        getContentResolver(), imageUri); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    }