ImageView.setImageBitmap是空白(ImageView.setImageBitm

2019-10-18 09:02发布

我用了一个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

}

}

Answer 1:

我不知道,但也有一些变化而获得的图像从相机的一部分onActivityResult()的API版本17.you通过使用获得的ImagePath形象正在努力。 但实际上相机不其存储给出name.It使用它自己的机制来存储image.You可以进入画廊看到。 图像名称会比你给什么不同。 根据建议最好是使用ContentResolver的查询是最后captured.You可以修改查询按照您的要求的图像。 下面只是一个例子。

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

            }


        }

    }


Answer 2:

在我自己的代码,我做尝试从媒体URI阅读之前以下电话:

activity.getContentResolver().notifyChange(mediaUri, null);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(mediaUri);
activity.sendBroadcast(intent);

这是为了保证系统正常更新,解决了内容URI的尝试。

编辑:对不起,我注意到你正在尝试从文件路径,而不是URI阅读。 我不知道为什么这可能会失败(不是权限问题等),但也许你可以尝试访问通过URI从URI.fromFile(镜像文件)中获得的文件。



Answer 3:

这对我的作品:

Intent intent = new Intent(
                            MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,
                            mCapturedImageURI);
                    startActivityForResult(intent, TAKE_PICTURE)

其次是:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    String imagePath = null;                
        if (requestCode == TAKE_PICTURE)
            if (resultCode == Activity.RESULT_OK) {                 
                imagePath = getPath(mCapturedImageURI);
            }
    }

    if (imagePath != null) {
        // TODO: to this from asynctask
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(imagePath);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        mBabymScaledPhoto = mApp.getBabyBitmapFromPath(imagePath,
                orientation);
        ImageView iv = (ImageView) fragView
                .findViewById(R.id.welcome_iv_babyPhoto);
        iv.setImageBitmap(mBabymScaledPhoto);
    }

}
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    CursorLoader cursorLoader = new CursorLoader(fragView.getContext(),
            uri, projection, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();
    if (cursor != null) {
        // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
        // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else
        return null;
}


文章来源: ImageView.setImageBitmap is blank