繁体   English   中英

从文件路径创建位图/可绘制对象

[英]Create a Bitmap/Drawable from file path

我正在尝试从现有文件路径创建位图或 Drawable。

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()不显示路径中的图像。 我用mText打印了路径,它看起来像: /storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

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

从文件路径创建位图:

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函数。

我认为您提供了错误的文件路径。 :) 希望这可以帮助。

这个对我有用:

File imgFile = new  File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    //Drawable d = new BitmapDrawable(getResources(), myBitmap);
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}

编辑:

如果上面的硬编码 sdcard 目录在您的情况下不起作用,您可以获取 sdcard 路径:

String sdcardPath = Environment.getExternalStorageDirectory().toString();
File imgFile = new  File(sdcardPath);

这是一个解决方案:

Bitmap bitmap = BitmapFactory.decodeFile(filePath);

好吧,使用静态Drawable.createFromPath(String pathName)对我来说似乎比自己解码更直接...... :-)

如果你的mImg是一个简单的ImageView ,你甚至不需要它,直接使用mImg.setImageUri(Uri uri)

static ArrayList< Drawable>  d;
d = new ArrayList<Drawable>();
for(int i=0;i<MainActivity.FilePathStrings1.size();i++) {
  myDrawable =  Drawable.createFromPath(MainActivity.FilePathStrings1.get(i));
  d.add(myDrawable);
}
对于 Drawable -
 Drawable drawable = Drawable.createFromPath(your path in string);
对于位图 -
 Bitmap bitmap = BitmapFactory.decodeFile(your path in string);

多么简单希望你喜欢

您无法通过路径访问您的可绘制对象,因此如果您想要一个带有可通过编程方式构建的可绘制对象的人类可读界面。

在类中的某处声明一个 HashMap:

private static HashMap<String, Integer> images = null;

//Then initialize it in your constructor:

public myClass() {
  if (images == null) {
    images = new HashMap<String, Integer>();
    images.put("Human1Arm", R.drawable.human_one_arm);
    // for all your images - don't worry, this is really fast and will only happen once
  }
}

现在访问 -

String drawable = "wrench";
// fill in this value however you want, but in the end you want Human1Arm etc
// access is fast and easy:
Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable));
canvas.drawColor(Color .BLACK);
Log.d("OLOLOLO",Integer.toString(wrench.getHeight()));
canvas.drawBitmap(wrench, left, top, null);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM