[英]Retrieve image path from assets folder
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.open it doesn't show up. 它确实检索了文件,但是用我在mngr.open中替换的字符串没有显示。
Really need you guys to help up.Thanks. 真的需要你们帮忙。谢谢。
You don't need the AssetManager. 您不需要AssetManager。 You can do
你可以做
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.
您将无法利用Android资源管理系统。 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. 因此,除非您有充分的理由这样做,否则建议您看看使用
res
文件夹和资源系统。
Update: Explanation The BitmapFactory
has a method to decode a file via the decodeFile
method. 更新:解释
BitmapFactory
有一个方法可以通过decodeFile
方法解码文件。 That's point one. 这是第一点。 Android lets you access a file in the
assets
folder via the file:///android_asset/{path}
path. Android允许您通过
file:///android_asset/{path}
路径访问assets
文件夹中的file:///android_asset/{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
. 在您的情况下,可以通过
file:///android_asset/image/Malay/bullet.jpg
来访问/image/Malay/bullet.jpg处的assets
文件夹中的Image。
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 您的BulletImage
String url = "file:///android_asset/NewFile.txt";
String url = "file:///android_asset/logo.png";
you can access any file.... 您可以访问任何文件...。
InputStream BulletImgInput = mngr.open("file:///android_asset/image/Malay/bullet.png");
也许这对你有用。
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
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.