从文件创建路径位图/绘制对象 [英] Create a Bitmap/Drawable from file path

查看:174
本文介绍了从文件创建路径位图/绘制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个位图或绘制对象从现有的文件的路径。

I'm trying to create a Bitmap or Drawable from existing file path.

String path = intent.getStringExtra("FilePath");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPreferredConfig = Bitmap.Config.ARGB_8888;

mImg.setImageBitmap(BitmapFactory.decodeFile(path));
// mImg.setImageBitmap(BitmapFactory.decodeFile(path, option));
// mImg.setImageDrawable(Drawable.createFromPath(path));
mImg.setVisibility(View.VISIBLE);
mText.setText(path);

setImageBitmap() setImageDrawable()不显示从路径的图像。我打印路径与多行文字,它看起来像: /storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

But setImageBitmap(), setImageDrawable() doesn't show an image from the path. I've printed path with mText and it looks like : /storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

我究竟做错了什么?任何人都可以帮我吗?

What am i doing wrong? Anyone can help me?

推荐答案

从文件创建路径的位图:

Create bitmap from file path:

File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);

如果你想扩展位图到父的高度和宽度,然后使用 Bitmap.createScaledBitmap 的功能。

If you want to scale the bitmap to the parent's height and width then use Bitmap.createScaledBitmap function.

我觉得你给了错误的文件路径。 :)希望这会有所帮助。

I think you are giving the wrong file path. :) Hope this helps.

这篇关于从文件创建路径位图/绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆