1

画像データをキャプチャした後、アプリに戻そうとしています。ただし、アプリに戻ろうとすると常にクラッシュします。

インテントを開始するためのコードは次のとおりです。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgView = (ImageView) findViewById(R.id.ImageView);
    upload = (Button) findViewById(R.id.Upload);
    snap = (Button) findViewById(R.id.Snap);
    select = (Button) findViewById(R.id.Select);
    subject = (EditText) findViewById(R.id.Subject);
    msg = (EditText) findViewById(R.id.Message);snap.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
             String imageFilePath = Environment.getExternalStorageDirectory().
             getAbsolutePath() + "/picture.jpg";  
             File imageFile = new File(imageFilePath); 
             Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri

             Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
             it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
             startActivityForResult(it, CAMERA_RESULT);
            }
        });
}

インテントを受け取るコードは次のとおりです。

 case CAMERA_RESULT:
        if (resultCode == Activity.RESULT_OK) {

                    // Get Extra from the intent 
                    Bundle extras = data.getExtras(); 
                    // Get the returned image from extra 
                    Bitmap bmp = (Bitmap) extras.get("data"); 

                    imgView = (ImageView) findViewById(R.id.ImageView); 
                    imgView.setImageBitmap(bmp); 
              }
        break;

アプリのクラッシュが発生したときに、次の例外も受け取ります。

java.lang.RuntimeException: アクティビティを再開できません {com.NYP.estatemanagement/com.NYP.estatemanagement.MainActivity}: java.lang.RuntimeException: 結果の配信に失敗しました ResultInfo{who=null, request=100, result=-1, data=null} をアクティビティ {com.NYP.estatemanagement/com.NYP.estatemanagement.MainActivity} に: java.lang.NullPointerException

4

2 に答える 2

0

これ(以下)@Biraj Zalavadia(回答)から:

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;
    }

String selectedImagePath = getAbsolutePath(data.getData());

私が必要としていたのはそれだけです

于 2014-01-07T04:32:38.947 に答える