Android 按路径显示图像
我想在 imageview 中显示图像而不使用 id。
我将所有图像放在原始文件夹中并打开,
try {
String ss = "res/raw/images/inrax/3150-MCM.jpg";
in = new FileInputStream(ss);
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
image.setImageBitmap(bMap);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
但这不起作用我想使用其路径而不是名称来访问图像
i want to show image in imageview without using id.
i will place all images in raw folder and open
try {
String ss = "res/raw/images/inrax/3150-MCM.jpg";
in = new FileInputStream(ss);
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
image.setImageBitmap(bMap);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
but this is not working i want to access image using its path not by name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 openRawResource() 读取字节流,
这样的事情应该可以工作
检查此链接
http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromCode
它清楚地说明了以下内容
虽然不常见,但您可能需要访问原始文件和目录。如果您这样做,那么将文件保存在 res/ 中将不起作用,因为从 res/ 读取资源的唯一方法是使用资源 ID
如果您想提供一个像上面提到的那样的文件名在您的代码中,您可能需要将其保存在资产文件夹中。
read a stream of bytes using openRawResource()
some thing like this should work
Check this link
http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromCode
It clearly says the following
While uncommon, you might need access your original files and directories. If you do, then saving your files in res/ won't work for you, because the only way to read a resource from res/ is with the resource ID
If you want to give a file name like the one mentioned in ur code probably you need to save it on assets folder.
您也许可以将
Resources.getIdentifier(name, type, package)
与原始文件一起使用。这将为您获取 id,然后您可以继续使用 setImageResource(id) 或其他任何操作。是你想要的吗?虽然它可能不喜欢多个文件夹,但值得一试。
You might be able to use
Resources.getIdentifier(name, type, package)
with raw files. This'll get the id for you and then you can just continue with setImageResource(id) or whatever.is what you want? It might not like the multiple folders though, but worth a try.
尝试 {
// 获取 AssetManager 的引用
AssetManager mngr = getAssets();
这里的图像目录是资产的路径,
例如
asset ->图像 ->某个文件夹 -> some.jpg
那么路径将是
image/somefolder/some.jpg
现在不需要图像的资源 id,您可以使用它在运行时填充图像
try {
// Get reference to AssetManager
AssetManager mngr = getAssets();
here image directory is path of assets
like
assest -> image -> somefolder -> some.jpg
then path will be
image/somefolder/some.jpg
now no need of resource id for image , you can populate image on runtime using this