从uri检索位图

Rob*_*ert 15 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 ...
}
Run Code Online (Sandbox Code Playgroud)

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

Chi*_*iya 37

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

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);
Run Code Online (Sandbox Code Playgroud)

  • @chintan khetiya不幸的是,它会向我返回FileNotFoundException (3认同)
  • 最好解释为什么这段代码是答案. (2认同)

nik*_*kvs 8

从uri获取位图,

Bitmap  mBitmap = Media.getBitmap(this.getContentResolver(), uri);
Run Code Online (Sandbox Code Playgroud)

希望这对你有所帮助.

  • 还记得从非主线程(后台线程)调用它 (8认同)