Android 从 uri 中检索位图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20782713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:27:19  来源:igfitidea点击:

Retrieve bitmap from uri

androidandroid-bitmap

提问by Robert

I have included 'share via myApp' option. I inserted following code in the receiving activity class.

我已经包含了“通过 myApp 共享”选项。我在接收活动类中插入了以下代码。

    // Get the intent that started this activity
    Intent intent = getIntent();
    Uri data = intent.getData();

    // Figure out what to do based on the intent type
    if (intent.getType().indexOf("image/") != -1) {
        // Handle intents with image data ...
}

What is the next step to retrieve bitmap image.

检索位图图像的下一步是什么。

回答by Chintan Khetiya

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

因为你已经得到了 Uri。现在您必须传入该 UrigetBitmap()以获取位图并使用该位图。

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

回答by nikvs

For getting bitmap from uri,

为了从 uri 获取位图,

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

Hope this helps you.

希望这对你有帮助。

回答by Er. Praful Parmar

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

回答by Faxriddin Abdullayev

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

回答by Jose

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.

你可以试试这个。您可以在 onActivityResult 方法中调用 setPic()。我已经在应用程序中使用它来拍照并将其放入 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);

}

回答by rupesh

Please prefer this link.

请优先选择此链接。

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

这就是您要寻找的 如何从 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;
        }