从uri检索位图

时间:2013-12-26 09:54:52

标签: android android-bitmap

我已添加'通过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 ...
}

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

8 个答案:

答案 0 :(得分:36)

因为你已经得到了Uri。现在你必须在getBitmap()中传递该Uri来获取位图并使用该位图。

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

答案 1 :(得分:7)

从uri获取位图,

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

希望这会对你有所帮助。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

这对我有用

document.getElementById("t-option").addEventListener("click", function () {
    setTimeout(function () {
        myFunction(this);
    }, 1500)
});

答案 4 :(得分:0)

您可以尝试一下。您可以在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);

}

答案 5 :(得分:0)

拥有 Uri 后,您可以使用以下内容:-

fun getBitmapFromUri(context: Context, uri: Uri): Bitmap? {
        
        var parcelFileDescriptor: ParcelFileDescriptor? = null
        return try {
            parcelFileDescriptor = context.contentResolver.openFileDescriptor(uri, "r")
            val fileDescriptor: FileDescriptor? = parcelFileDescriptor?.fileDescriptor
            val image = BitmapFactory.decodeFileDescriptor(fileDescriptor)
            parcelFileDescriptor?.close()
            image
        } catch (e: Exception) {
            //Log.e(TAG, "Failed to load image.", e)
            null
        } finally {
            try {
                parcelFileDescriptor?.close()
            } catch (e: IOException) {
                e.printStackTrace()
                //Log.e(TAG, "Error closing ParcelFile Descriptor")
            }
        }
    }

答案 6 :(得分:0)

参考 Android Developers documentation,您可以使用它从 URI 中获取位图

    ....

    Uri uri = intent.getData();
    try {
        Bitmap bitmap = getBitmapFromUri(uri);
        // do something with this bitmap

    } catch (IOException e) {
        e.printStackTrace();
    }
}


private Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();
    return image;
}

答案 7 :(得分:-4)

请选择此链接。

这就是你要找的东西 How to get Bitmap from an Uri?

试试这个对我有用:

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