在安卓系统中将图片转换为位图

6

我希望能从手机的SD卡中选择一张图片。我正在使用下面的代码进行选择并在我的活动中显示:

Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Uri uri = Uri.parse(selectedImagePath);
uploadimage.setImageURI(uri);

这个功能目前正常运作,但我想把这张图片转换成Bitmap格式,我已经有了图片的路径和URI。

在这种情况下如何将图片转换为Bitmap格式呢?请帮助我,谢谢!


使用此链接:http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/2/。 - Uttam
您可以在此处阅读有关使用位图的内容:http://maximbogatov.wordpress.com/2011/08/03/bitmaps-in-android/。其中包含如何从路径获取位图的示例。 - Maxim
2个回答

21

使用这段代码

Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

太棒了,很高兴你发布了这个。 - dylan murphy
需要一个try catch块,但是答案很好。 - Yaroslav Dukal

-1
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);

已弃用.... - Yaroslav Dukal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接