2012-03-14 11 views
3

私はAndroidのカメラの意図と選択写真の意図(2つのボタン、写真を撮るためのもの、ギャラリーからの選択のためのもの)を起動しようとしています。彼らの後に起動された意図は、クロップされた写真を私のアプリの活動に戻した。私は他の場所に掲載されているサンプルの束を見てきましたが、私の実装では奇妙な結果を得ています。Android - カメラとギャラリーの意図が変わった後に切り取る

写真撮影のイベントでは、写真を撮ってトリミングモードに入ると、間違った写真がポップアップして表示されます。ちょうど撮った写真を切り取るのではなく、古い写真を切り取って、どこから来ているのか分かりません。また、作物の意図が終了した後に、Parcel.readExceptionの後にnullpointerexceptionでクラッシュすることもあります(必ずしも再現できませんが、できるだけ早く作画してください)。

写真を選択すると、ギャラリーが期待どおりにポップアップしますが、写真を選択すると、画像とともにアプリのアクティビティに戻る代わりに「保存済み」というメッセージが表示されます。私は、写真の意図をどのように選択するかについて誤解していると思います(私はかなり写真の意図を表すコードを再利用しました)。

どちらの場合も、クロップモードでは、 "scale" = falseを指定したにもかかわらず、クロップされた領域のサイズを変更することができます。次のように

私のコードは次のとおりです。

public class TestPhotoActivity extends Activity { 

private ImageView imageView; 
private Uri imageUri; 

private int int_Height_crop = 600; 
private int int_Width_crop = 600; 

public final static int TAKE_PICTURE = 0; 
public final static int CHOOSE_PICTURE = 1; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.choose_photo); 

    imageView = (ImageView) findViewById(R.id.photo); 

    Button take_photo = (Button) findViewById(R.id.take_photo); 
    take_photo.setOnClickListener(new View.OnClickListener() { 
     public void onClick(final View view) { 
      takePhoto(view); 
     } 
    }); 

    Button choose_photo = (Button) findViewById(R.id.choose_photo); 
    choose_photo.setOnClickListener(new View.OnClickListener() { 
     public void onClick(final View view) { 
      choosePhoto(view); 
     } 
    });  

} 

public void takePhoto(View view) {  
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE", null); 
    intent.putExtra("crop", "true"); 
    intent.putExtra("aspectX", 1); 
    intent.putExtra("aspectY", 1); 
    intent.putExtra("outputX", int_Width_crop); 
    intent.putExtra("outputY", int_Height_crop); 
    intent.putExtra("scale", false); 
    intent.putExtra("return-data", true); 
    File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");  
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 
    imageUri = Uri.fromFile(photo); 
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); 
    intent.putExtra("noFaceDetection", true); 
    startActivityForResult(intent, TAKE_PICTURE); 
} 

public void choosePhoto(View view) {   
    Intent intent = new Intent(Intent.ACTION_PICK, null); 
    intent.setType("image/*"); 
    intent.putExtra("crop", "true"); 
    intent.putExtra("aspectX", 1); 
    intent.putExtra("aspectY", 1); 
    intent.putExtra("outputX", int_Width_crop); 
    intent.putExtra("outputY", int_Height_crop); 
    intent.putExtra("scale", false); 
    intent.putExtra("return-data", true); 
    File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + "Pic.jpg");   
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 
    imageUri = Uri.fromFile(photo); 
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); 
    startActivityForResult(intent, CHOOSE_PICTURE);   
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) {   
     case TAKE_PICTURE:    
      Log.d("photo", "requestCode: " + requestCode + "resultCode: " + resultCode + "wanted result: " + Activity.RESULT_OK);    
      if (resultCode == Activity.RESULT_OK) { 

       if (data == null) { 
        Log.w("photo", "Null data, but RESULT_OK, from image picker!"); 
        Toast t = Toast.makeText(this, "No photo picked.", Toast.LENGTH_SHORT); 
        t.show(); 
        return; 
       } 

       final Bundle extras = data.getExtras(); 

       if (extras != null) { 
        Log.d("photo", "extras is not null"); 
        Uri selectedImage = imageUri; 
        getContentResolver().notifyChange(selectedImage, null);     
        ContentResolver cr = getContentResolver(); 
        Bitmap bitmap; 
        try { 
         bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);  
         Log.d("photo", "data.getAction() is not null. setting image."); 
         imageView.setImageBitmap(bitmap);       
        } catch (Exception e) { 
         Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show(); 
         Log.e("photo", e.toString()); 
        } 
       } 
      } 
     case CHOOSE_PICTURE: 
      Log.d("photo", "requestCode: " + requestCode + "resultCode: " + resultCode + "wanted result: " + Activity.RESULT_OK); 
      if(resultCode == RESULT_OK){ 
       Log.d("photo", "resultCode is ok"); 

       if (data == null) { 
        Log.w("photo", "Null data, but RESULT_OK, from image picker!"); 
        Toast t = Toast.makeText(this, "No photo picked.", Toast.LENGTH_SHORT); 
        t.show(); 
        return; 
       } 

       final Bundle extras = data.getExtras(); 

       if (extras != null) { 
        Log.d("photo", "extras is not null"); 
        Uri selectedImage = imageUri; 
        getContentResolver().notifyChange(selectedImage, null);     
        ContentResolver cr = getContentResolver(); 
        Bitmap bitmap; 
        try { 
         bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage); 
         Log.d("photo", "data.getAction() is not null. setting image."); 
         imageView.setImageBitmap(bitmap); 
        } catch (Exception e) { 
         Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show(); 
         Log.e("photo", e.toString()); 
        } 
       }     

      } 
    } 
} 

} 

すべてのヘルプは大歓迎です!

編集:私はまた、私はあなたの問題は一時ファイル名でのSystem.currentTimeMillis()をuseingから来ていると思う私はLGオプティマスLTE、アンドロイド2.3

+0

...それがお役に立てば幸い正常に動作しますが、私のアプリのユーザーの多くがこれに奇妙な問題を抱えている...あなたはどんな解決策を見つける必要がありますか? – Igor

答えて

0

にテストしてることに注意してください。これは時には古いファイルを取得することを説明します。

同じ一時ファイルを再利用することをお勧めします。

は私のサムスンi9000の中で、同じ問題

関連する問題