我应该对bitmapfactory.decodeFile(string pathname)使用哪种路径格式? [英] What path format should I use with bitmapfactory.decodeFile(string pathname)?

查看:574
本文介绍了我应该对bitmapfactory.decodeFile(string pathname)使用哪种路径格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用功能bitmapfactory.decodeFile(string pathname).

但是在使用它时,将返回NULL. 请举例说明写路径名的内容或方式.

But on using it, NULL is being returned. Please specify what or how to WRITE the PATHNAME, with an example.

for(int j=0;j<3;j++)
{
    img_but[count].setImageBitmap(BitmapFactory.decodeFile("/LogoQuiz/res/drawable-hdpi/logo0.bmp"));
    linear[i].addView(img_but[count]);
    count++;
}

应该使用什么代替这种路径名?

Instead of such a pathname, what should be used?

推荐答案

如果要使用文件名,则不能将它们放在可绘制文件夹中.您执行此操作的唯一方法是将图像放在资产文件夹中.如果没有assets/文件夹,则必须在主项目中创建一个文件夹.它应该与gen/res/src/文件夹位于同一分支.

If you want to use filenames, then you can not put them inside the drawable folder. The only way you can do this is by putting the images inside the assets folder. If you do not have an assets/ folder, then you must create one inside your main project. It should be in the same branch as your gen/, res/, and src/ folders.

您可以在资产文件夹中拥有所需的任何文件结构.因此,例如,您可以将图像放在assets/images/文件夹中.声音文件可以放在assets/sounds/文件夹中.您可以像这样访问图像:

You can have any file structure in your assets folder you want. So for example, you can put your images in the assets/images/ folder. Sound files can go in a assets/sounds/ folder. You access an image like so:

public Bitmap getBitmap(Context ctx, String pathNameRelativeToAssetsFolder) {
  InputStream bitmapIs = null;
  Bitmap bmp = null;
  try {
    bitmapIs = ctx.getAssets().open(pathNameRelativeToAssetsFolder);
    bmp = BitmapFactory.decodeStream(bitmapIs);
  } catch (IOException e) {
    // Error reading the file
    e.printStackTrace();

    if(bmp != null) {
      bmp.recycle();
      bmp = null
    }
  } finally {
    if(bitmapIs != null) {
       bitmapIs.close();
    }
  }

  return bmp;
}

路径名(如变量名所示)应相对于assets/文件夹.因此,如果您将图像直接放在文件夹中,则只需imageName.png.如果在子文件夹中,则为subfolder/imageName.png.

The path name, as the variable name suggests, should be relative to the assets/ folder. So if you have the images straight in the folder, then it's simply imageName.png. If it's in a subfolder, then it's subfolder/imageName.png.

注意:Android不会从资产文件夹的密度文件夹中进行选择.它按原样解码图像.屏幕密度和分辨率的任何进一步调整都必须由您完成.

Note: Android will not choose from density folders in the assets folder. It decodes the images as-is. Any further adjustments for screen density and resolution will have to be done by you.

从android中的Assets文件夹中打开文件

这篇关于我应该对bitmapfactory.decodeFile(string pathname)使用哪种路径格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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