0

私は、ユーザーがそのアクティビティのImageViewに画像を追加できるようにするためのアクティビティを作成しました。アクティビティのコンテキストメニューを開くボタンを作成しました。は、ギャラリーから写真を選択するか、カメラで写真を撮るオプションをユーザに提供します。

最初のオプションを選択した場合 - ギャラリーから写真を選択すると問題なく動作します。ギャラリーが開かれ、画像が選択され、アクティビティが再開され、画像がImageViewに追加されます。

奇妙なことは、絵を取り、私の活動に再開し、第二の選択肢を選択した後起こっ開始:
Android - 画像ギャラリーの代わりにカメラアクティビティが開かれる

  1. 私は再びコンテキストメニューを開き、ギャラリーを開こうとすると、カメラ 活動が開かれます代わりに
  2. 私は、ダイアログは私がギャラリーを開く
  3. を示している「を使用して完全なアクション」を、カメラのアクティビティを閉じて、私の活動に再開し、画像を選択し、NullPointerExceptionがスローされます

なぜこの動作と例外はありますか?私は同様のトピックを検索しようとしましたが、解決策が見つかりませんでした。以下
は、ここでは、私は私の活動からカメラアクティビティを呼び出すために使用され、それがうまく働いていた私の活動

public boolean onContextItemSelected(MenuItem item) { 
    super.onContextItemSelected(item); 
    switch(item.getItemId()) { 
    case R.id.cm_Select_picture: { 
     // TODO open gallery 
     Intent intent = new Intent(); 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     startActivityForResult(Intent.createChooser(intent, ""), RC_SELECT_PICTURE); 
    } 
    case R.id.cm_Take_picture: { 
     // TODO open camera 
     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraIntent, RC_TAKE_PICTURE); 
    } 
    default: return false; 
    } 
} 


protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(resultCode == RESULT_OK) { 
     switch(requestCode) { 
     case RC_SELECT_PICTURE: { 
      Log.d(TAG, "Case select picture"); 
      Uri selectedImageUri = data.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImageUri 
        , filePathColumn, null, null, null); 
      cursor.moveToFirst(); 
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String filePath = cursor.getString(columnIndex); 
      cursor.close(); 
      Bitmap pic = BitmapFactory.decodeFile(filePath); 
      goodsImage.setImageBitmap(pic); 
     } 
     case RC_TAKE_PICTURE: { 
      Log.d(TAG, "Case take picture"); 
      if(data.getExtras().get("data") != null) { 
       Bitmap pic = (Bitmap) data.getExtras().get("data"); 
       goodsImage.setImageBitmap(pic); 
      } 
     } 
     } 
    } 
} 


04-26 01:34:59.529: E/AndroidRuntime(20531): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=content://media/external/images/media/9 }} to activity {com.forestassistant/com.statistics.GoodsActivity}: java.lang.NullPointerException 

答えて

1

する方法です私のコードは

です
setMediaUri(getNewMediaFilePath(actOwner.getContentResolver())); 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getMediaUri()); 
       startActivityForResult(cameraIntent, CAMERA_CAPTURE_REQUEST_CODE); 

とするとき、結果のコールバック

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK 
      && requestCode == CAMERA_CAPTURE_REQUEST_CODE) { 
     Drawable toRecycle= imgView.getDrawable(); 
     if (toRecycle != null) {     
      ((BitmapDrawable)imgView.getDrawable()).getBitmap().recycle(); 
     } 
     mImg = decodeFileIntoRequiredSize(getPath(getMediaUri(),this), requiredSizeForImage); 
     imgView.setImageBitmap(mImg);   
    } 
public Bitmap decodeFileIntoRequiredSize(String filePath,int requiredSize){ 
    Bitmap b = null; 
    try { 
     File f = new File(filePath); 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 

     FileInputStream fis = new FileInputStream(f); 
     BitmapFactory.decodeStream(fis, null, o); 
     fis.close(); 
     int scale = 1; 
     if (o.outHeight > requiredSize || o.outWidth > requiredSize) { 
      scale = (int)Math.pow(2, (int) Math.round(Math.log(requiredSize/(double) Math.max(o.outHeight, o.outWidth))/Math.log(0.5))); 
     } 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     fis = new FileInputStream(f); 
     b = BitmapFactory.decodeStream(fis, null, o2); 
     fis.close(); 
    } catch (IOException e) { 
    } 
    return b; 
} 
public String getPath(Uri uri,Activity act) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = act.managedQuery(uri, projection, null, null, null); 
    if (cursor != null) {  
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } else 
     return null; 
} 
public Uri getNewMediaFilePath(ContentResolver contentResolver) { 
    ContentValues values = new ContentValues(); 

    //create the directory 
    // /mnt/sdcard/DCIM/Camera/IMG_20111101_111922.jpg 
    String cameraDir = "/Camera"; 
    //File dir1 = act.getExternalFilesDir(Environment.DIRECTORY_DCIM); 
    File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()+cameraDir); 
    if(!dir.exists()) dir.mkdir(); 

    //use date as filename 
    String name = "IMG_" + (String) android.text.format.DateFormat.format("yyyyMMdd_hhmmss",new Date()); 
    String path = dir.getPath()+File.separator + name; 
    values.put(MediaStore.MediaColumns.TITLE, name); 
    values.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis()); 
    Uri base = null; 
    path += ".jpg"; 
    base = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
    values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg"); 
    values.put(MediaStore.MediaColumns.DATA, path); 
    return contentResolver.insert(base, values); 
} 

あなたはこのヘルプを願っています。

+0

私は、それぞれのケースステートメントで休憩を追加するのを忘れてしまったことに気がつきました。今は期待どおりに動作します。とにかく、あなたの答えをありがとう! – droid8421

関連する問題