2013-06-26 81 views
0

我想使用功能bitmapfactory.decodeFile(string pathname)我应该使用什么路径格式与bitmapfactory.decodeFile(字符串路径名)?

但是在使用它时,NULL正在返回。 举例说明,请指定写入PATHNAME的内容或方法。

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++; 
} 

而不是这样的路径名,应该使用什么?

+0

你不能在res文件夹中指定一个路径名。使用decodeResource(getResources(),R.drawable.logo0); – DeeV

+0

Wad是我想只使用decodeFile(String pathName)? 请帮助与Wad PathName代表! 我有300多个图像,我想使用一个字符串路径名与循环变量连接n减少我的代码的长度... – Vaido

回答

1

如果你想使用文件名,那么你不能把它们放在drawable文件夹中。您可以做到这一点的唯一方法是将图像放入资产文件夹中。如果您没有assets/文件夹,则必须在您的主项目中创建一个文件夹。它应该与您的,res/src/文件夹处于同一分支。

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

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

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

Opening a File from assets folder in android

+0

SUPERLIKE !! .. ... 非常感谢... :) – Vaido