相机意图图片文件路径

时间:2013-02-21 18:24:32

标签: android path image

我正在尝试将相机拍摄的照片路径作为字符串。当我尝试使用我的代码拍摄图片时,字符串为空。当我关闭我的应用程序时,打开相机应用程序并在那里拍照,然后重新打开我的应用程序并使用我的代码然后我得到我用相机应用程序拍摄的最后一张照片的字符串。我的问题是为什么我的相机意图不起作用,为什么它在我使用相机应用程序时有效?

这是我的代码:

public void onClick(View arg0) {
    switch (arg0.getId()) {     
      case R.id.btnImageCapture:
        Intent openCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(openCamera, OPEN_CAMERA); 
                    break;
    }
}

private String getLastImagePath() {
            final String[] imageColumns = { MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.DATA };
            final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
            Cursor imageCursor = managedQuery(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
                    null, null, imageOrderBy);
            if (imageCursor.moveToFirst()) {
                String fullPath = imageCursor.getString(imageCursor
                        .getColumnIndex(MediaStore.Images.Media.DATA));
                imageCursor.close();
                return fullPath;
            }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         switch(requestCode){
         case OPEN_CAMERA:
             if (resultCode == RESULT_OK && data != null) {
                 String picturePath = getLastImagePath(); 
                 break;
             } 
             }
        }

1 个答案:

答案 0 :(得分:2)

试试这个。它有效..

  private Uri imageUri;

  public void onClick(View arg0) {
    switch (arg0.getId()) {     
      case R.id.btnImageCapture:

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, OPEN_CAMERA);
                    break;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case OPEN_CAMERA:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ImageView imageView = (ImageView) findViewById(R.id.ImageView);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                 bitmap = android.provider.MediaStore.Images.Media
                 .getBitmap(cr, selectedImage);

                imageView.setImageBitmap(bitmap);
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();

            }
        }
    }
}