Android 从资产文件夹中检索图像路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10597398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve image path from assets folder
提问by Yuen Tong
I have issue to retrieve image path from assets folder.
我有问题从资产文件夹中检索图像路径。
I have an image folder in assets folder. Within the folder i have three different folders.
我在资产文件夹中有一个图像文件夹。在文件夹中,我有三个不同的文件夹。
Here is the code that i used:
这是我使用的代码:
String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ;
try{
AssetManager mngr =getAssets();
InputStream BulletImgInput = mngr.open(IntroImage1);
//InputStream BulletImgInput = mngr.open("image/Malay/bullet.png");
Bitmap bitmapBullet = BitmapFactory.decodeStream(BulletImgInput);
BulletImage.setImageBitmap(bitmapBullet);
}catch(final IOException e){
e.printStackTrace();
}
I am wondering why can't i display the image? Because I have try to retrieve it through this code:
我想知道为什么我不能显示图像?因为我尝试通过此代码检索它:
InputStream BulletImgInput = mngr.open("image/Malay/bullet.png");
It did retrieve the file, but with the string that i replaced in mngr.openit doesn't show up.
它确实检索了文件,但是我在mngr.open 中替换的字符串没有显示出来。
Really need you guys to help up.Thanks.
真的需要你们帮忙。谢谢。
回答by Vikram Bodicherla
You don't need the AssetManager. You can do
您不需要 AssetManager。你可以做
BitmapFactory.decodeFile("file:///android_asset/image/Malay/bullet.jpg")
BitmapFactory.decodeFile("file:///android_asset/image/Malay/bullet.jpg")
Though storing Images in the Assets is not the best way to go. You will not be able to take advantage of the Android resource management system. So unless you have a compelling reason to do this, I'd suggest that you take a look at using the res
folder and the resources system.
尽管将图像存储在资产中并不是最好的方法。您将无法利用 Android 资源管理系统。因此,除非您有令人信服的理由这样做,否则我建议您考虑使用res
文件夹和资源系统。
Update: Explanation
The BitmapFactory
has a method to decode a file via the decodeFile
method. That's point one. Android lets you access a file in the assets
folder via the file:///android_asset/{path}
path. In your case, the Image at /image/Malay/bullet.jpg is the assets
folder can be accessed via the the file:///android_asset/image/Malay/bullet.jpg
.
更新: 说明BitmapFactory
有一种通过该decodeFile
方法解码文件的方法。这是第一点。Android 允许您assets
通过file:///android_asset/{path}
路径访问文件夹中的文件。在您的情况下,位于 /image/Malay/bullet.jpg 的图像assets
是可以通过file:///android_asset/image/Malay/bullet.jpg
.
回答by K_Anas
Try this:
尝试这个:
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
Log.e("I/O ERROR","Failed when ..."
}
your BulletImage
你的子弹图片
回答by MAC
String url = "file:///android_asset/NewFile.txt";
String url = "file:///android_asset/logo.png";
you can access any file....
您可以访问任何文件....
回答by Thkru
InputStream BulletImgInput = mngr.open("file:///android_asset/image/Malay/bullet.png");
Maybe this could work for u.
也许这对你有用。
回答by Sergey Glotov
May be problem is in missed image
part of path?
可能是问题出在image
路径的缺失部分?
String IntroImage1= "image/" + languageSelected + "/" + Intro1ImagePath + ".png" ;
instead of
代替
String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ;
Upd: Check values of languageSelected
and Intro1ImagePath
also.
更新:检查languageSelected
和的值Intro1ImagePath
。