0

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

这是我的代码:

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

1 回答 1

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();

            }
        }
    }
}
于 2013-02-21T18:32:15.560 回答