2013-09-27 49 views
21

我真的很沮喪解決我的問題,我有一個應用程序,使用相機,當相機捕獲一張照片,它將顯示在活動上,當我不使用時cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);照片將顯示在活動上,但名稱和路徑文件不像我想要的那樣。我使用的Galaxy Tab編譯它,我必須跟着教程來解決我的問題,從hereherehere但我的應用程序仍然強制關閉和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 

我真的希望有人能幫助我解決我的問題,我已經ş大頭貼,不能再反思了,請別人幫我......謝謝。

+0

這只是一個空指針異常,FormSpaj.java:993是什麼? –

+0

位圖photo =(位圖)data.getExtras()。get(「data」); –

+0

'getExtras()'可以返回null,如果沒有額外的Bundle。你應該確保'getExtras()'返回的值不是'null'。 – jedwards

回答

20

試試這個工作一樣的魅力與我

private String selectedImagePath = ""; 
    final private int PICK_IMAGE = 1; 
    final private int CAPTURE_IMAGE = 2; 

public Uri setImageUri() { 
     // Store image in dcim 
     File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png"); 
     Uri imgUri = Uri.fromFile(file); 
     this.imgPath = file.getAbsolutePath(); 
     return imgUri; 
    } 


    public String getImagePath() { 
     return imgPath; 
    } 

btnGallery.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE); 

      } 
     }); 

     btnCapture.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri()); 
       startActivityForResult(intent, CAPTURE_IMAGE); 
      } 
     }); 

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode != Activity.RESULT_CANCELED) { 
      if (requestCode == PICK_IMAGE) { 
       selectedImagePath = getAbsolutePath(data.getData()); 
       imgUser.setImageBitmap(decodeFile(selectedImagePath)); 
      } else if (requestCode == CAPTURE_IMAGE) { 
       selectedImagePath = getImagePath(); 
       imgUser.setImageBitmap(decodeFile(selectedImagePath)); 
      } else { 
       super.onActivityResult(requestCode, resultCode, data); 
      } 
     } 

    } 


public Bitmap decodeFile(String path) { 
     try { 
      // Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(path, o); 
      // The new size we want to scale to 
      final int REQUIRED_SIZE = 70; 

      // Find the correct scale value. It should be the power of 2. 
      int scale = 1; 
      while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE) 
       scale *= 2; 

      // Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      return BitmapFactory.decodeFile(path, o2); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     return null; 

    } 

public String getAbsolutePath(Uri uri) { 
     String[] projection = { MediaColumns.DATA }; 
     @SuppressWarnings("deprecation") 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     if (cursor != null) { 
      int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } else 
      return null; 
    } 
+0

謝謝你的幫助......但我有一個問題,爲什麼當我捕捉另一個圖像時,這不是重寫之前創建的圖像? –

+0

,因爲我們通過追加當前時間毫秒來在此方法setImageUri()中生成唯一的uri。如果要在setImageUri()方法中覆蓋該文件,請提供靜態硬編碼文件名。 –

+0

我這樣做了:File file = new File(Environment.getExternalStorageDirectory()+「/ android/data/spaj_foto/spaj_foto.png」); Uri imgUri = Uri.fromFile(file); this.imgPath = file.getAbsolutePath(); –

6

試試這個,昨天我有同樣的問題。使用下面的代碼我糾正了問題。試試吧。

注意:並且還爲特定活動添加configChanges。同時調用相機意圖活動將被刷新。你需要避免這種情況。 android:configChanges="orientation|keyboardHidden|screenSize"

btnGallery.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      intent = new Intent(Intent.ACTION_PICK); 
          intent.setType("image/*"); 
          startActivityForResult(intent, 1); 
     } 
    }); 

    btnCapture.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(
            MediaStore.ACTION_IMAGE_CAPTURE); 
          File file = new File(Environment 
            .getExternalStorageDirectory(), 
            "test.jpg"); 

          outputFileUri = Uri.fromFile(file); 
          Log.d("TAG", "outputFileUri intent" 
            + outputFileUri); 
          intent.putExtra(MediaStore.EXTRA_OUTPUT, 
            outputFileUri); 
          startActivityForResult(intent, 0); 
     } 
    }); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    switch (requestCode) { 
    case 0: 
     if (resultCode == RESULT_OK) { 
      Log.d("TAG", "outputFileUri RESULT_OK" + outputFileUri); 
      if (outputFileUri != null) { 

       Bitmap bitmap; 
       bitmap = decodeSampledBitmapFromUri(outputFileUri, 
         image.getWidth(), image.getHeight()); 

       if (bitmap == null) { 
        Toast.makeText(getApplicationContext(), 
          "the image data could not be decoded", 
          Toast.LENGTH_LONG).show(); 

       } else { 
        Toast.makeText(
          getApplicationContext(), 
          "Decoded Bitmap: " + bitmap.getWidth() + " x " 
            + bitmap.getHeight(), Toast.LENGTH_LONG) 
          .show(); 
        image.setImageBitmap(bitmap); 
       } 
      } 
     } 
     break; 
    case 1: 
     if (resultCode == RESULT_OK) { 
      Uri targetUri = data.getData(); 
      Log.d("TAG", "datae" + targetUri); 
      Bitmap bitmap; 
      bitmap = decodeSampledBitmapFromUri(targetUri, 
        image.getWidth(), image.getHeight()); 

      if (bitmap == null) { 
       Toast.makeText(getApplicationContext(), 
         "the image data could not be decoded", 
         Toast.LENGTH_LONG).show(); 

      } else { 
       Toast.makeText(
         getApplicationContext(), 
         "Decoded Bitmap: " + bitmap.getWidth() + " x " 
           + bitmap.getHeight(), Toast.LENGTH_LONG) 
         .show(); 
       image.setImageBitmap(bitmap); 
      } 
     } 
     break; 

    default: 
     break; 
    } 
} 

public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, 
     int reqHeight) { 

    Bitmap bm = null; 

    try { 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(getContentResolver() 
       .openInputStream(uri), null, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     bm = BitmapFactory.decodeStream(getContentResolver() 
       .openInputStream(uri), null, options); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Toast.makeText(getApplicationContext(), e.toString(), 
       Toast.LENGTH_LONG).show(); 
    } 

    return bm; 
} 

public int calculateInSampleSize(BitmapFactory.Options options, 
     int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float) height/(float) reqHeight); 
     } else { 
      inSampleSize = Math.round((float) width/(float) reqWidth); 
     } 
    } 
    return inSampleSize; 
} 
3

更清晰的代碼,可以幫助別人尋找答案,基本上當你保存文件到外部存儲捆綁包不再有結果的位圖,而不是你必須從文件

String fileName; 
. 
. 
private void takePicture() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    // Ensure that there's a camera activity to handle the intent 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     // Create the File where the photo should go 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
      // Error occurred while creating the File 

     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
        Uri.fromFile(photoFile)); 
      startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
     } 
    } 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data)  { 
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 

     loadImageFromFile(); 
    } 
} 
public void loadImageFromFile(){ 

    ImageView view = (ImageView)this.findViewById(R.id.imageViewHeader); 
    view.setVisibility(View.VISIBLE); 


    int targetW = view.getWidth(); 
    int targetH = view.getHeight(); 

    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(fileName, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 

    // Determine how much to scale down the image 
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeFile(fileName, bmOptions); 
    view.setImageBitmap(bitmap); 
} 
private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    fileName = image.getAbsolutePath(); 
    return image; 
} 
+0

這有幫助。謝謝!我仍然試圖'位圖圖片=(位圖)data.getExtras()。獲得(「數據」);' – RichArt

2

我加載認爲以下行導致問題。

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

你已經通過的文件URI如下

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri); 

當您通過EXTRA_OUTPUT一個URI,你在onActivityResult接收(意圖)將是無效的。如果您想以此方法獲取圖像,請使用EXTRA_OUTPUT傳遞的路徑獲取圖像。

+0

這真的有效。我開始面對Android 7.0的崩潰問題,並刪除這條線沒有辦法。此外,刪除這條線也不會影響牛軋糖前的設備。從Android 4.4及以上版本進行測試。 –

相關問題