我使用MediaStore拍照并保存了。我可以在图库中看到该图像。但是,当我尝试将图像位图设置为ImageView时,它变为空白。

String abspath;

public void getPicture(View view) {

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imageFile = new File(PATH + "/image.jpg");
        abspath = imageFile.getAbsolutePath().toString();

        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(abspath)))

        startActivityForResult(cameraIntent, RESULT_CAMERA);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    setContentView(R.layout.activity);

    if(requestCode == RESULT_CAMERA) {
            // open the image file
            Bitmap bitmap = BitmapFactory.decodeFile(abspath);
            ImageView imageView = (ImageView)findViewById(R.id.imageView);

            imageView.setImageBitmap(bitmap); //imageView is now blank, although my saved image is not a blank image
            //imageView.setImageURI(Uri.fromFile(new File(abspath))); same result

}


}

最佳答案

我不知道,但是在API版本17中作为onActivityResult()的一部分从Camera获取图像时,有一些更改。您正在尝试使用imagePath获取图像。但实际上相机并没有按给定名称存储图像,而是使用其自身的机制来存储图像,您可以通过进入画廊来查看。图像名称将与您提供的名称不同。根据建议,最好使用ContentResolver查询最后捕获的图​​像。您可以根据需要修改查询。以下仅是示例。

 protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PIC_CAMERA_IMAGE && resultCode == RESULT_OK) {

            Uri selectedImage=null;
                    // Works on API 16 or Below
            if(null!=data)
            {
                selectedImage = data.getData();
            }
                    // For API 17
            if(null==selectedImage)
            {
                final ContentResolver cr = getContentResolver();
                final String[] p1 = new String[] {
                        MediaStore.Images.ImageColumns._ID,
                        MediaStore.Images.ImageColumns.DATE_TAKEN
                };
                Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
                if ( c1.moveToFirst() ) {
                    String uristringpic = "content://media/external/images/media/" +c1.getInt(0);
                    selectedImage = Uri.parse(uristringpic);
                }
                c1.close();

            }


        }

    }

10-08 06:14