Retrieve bitmap from uri

杀马特。学长 韩版系。学妹 提交于 2019-11-30 11:22:55

As you have already get the Uri. Now you have to pass that Uri in getBitmap() to get bitmap and use that bitmap.

Uri imageUri = intent.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
Imageview my_img_view = (Imageview ) findViewById (R.id.my_img_view);
my_img_view.setImageBitmap(bitmap);

For getting bitmap from uri,

Bitmap  mBitmap = Media.getBitmap(this.getContentResolver(), uri);

Hope this helps you.

Retrive bitmap from uri.....

public static Bitmap decodeUriToBitmap(Context mContext, Uri sendUri) {
        Bitmap getBitmap = null;
        try {
            InputStream image_stream;
            try {
                image_stream = mContext.getContentResolver().openInputStream(sendUri);
                getBitmap = BitmapFactory.decodeStream(image_stream);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getBitmap;
    }

This is work for me

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
    Uri imageUri = data.getData();
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
}

You can try this. You can call setPic() in onActivityResult method. I have used this in an application to take photos an put it in an ImageView.

private void setPic() {

    //currentPhotoPath contains path of image file.
    //visorFoto is a reference to an ImageView object.
    File file = new File(currentPhotoPath);
    Uri imageUri = Uri.fromFile(file);
    visorFoto.setImageURI(imageUri);

}
rupesh

Please prefer this link.

This is what you are looking for How to get Bitmap from an Uri?

Try this it works for me:

public static Bitmap getBitmapFromURL(String src) {
        try {
            System.out.printf("src", src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            System.out.printf("Bitmap", "returned");
            myBitmap = Bitmap.createScaledBitmap(myBitmap, 100, 100, false);//This is only if u want to set the image size.
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.printf("Exception", e.getMessage());
            return null;
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!