我想不使用id在imageview中显示图像。
我会把所有的图片放在raw文件夹中并打开
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());
}但是这不起作用,我想使用其路径而不是名称来访问图像
发布于 2010-09-12 07:02:03
尝试{ //获取对AssetManager的引用
AssetManager mngr = getAssets(); // Create an input stream to read from the asset folder
InputStream ins = mngr.open(imdir);
// Convert the input stream into a bitmap
img = BitmapFactory.decodeStream(ins);
} catch (final IOException e) {
e.printStackTrace();
} 其中图像目录是资产的路径
喜欢
assest -> image -> somefolder -> some.jpg (评估某个文件夹映像文件)
则路径将为
image/某文件夹/某.jpg
现在不需要图像的资源id,您可以在运行时使用以下内容填充图像
发布于 2010-09-11 22:29:07
使用openRawResource()读取字节流
像这样的事情应该是可行的
InputStream is = context.getResources().openRawResource(R.raw.urfilename);检查此链接
http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromCode
它清楚地说明了以下内容
虽然不常见,但您可能需要访问原始文件和目录。如果这样做,那么将文件保存在res/中将不会对您起作用,因为从res/读取资源的惟一方法是使用资源ID
如果你想给出一个类似于你的代码中提到的文件名,你可能需要把它保存在assets文件夹中。
发布于 2010-09-12 04:44:45
您也许能够对原始文件使用Resources.getIdentifier(name, type, package)。这将为您获取id,然后您可以继续使用setImageResource(id)或其他任何东西。
int id = getResources().getIdentifier("3150-MCM", "raw", getPackageName());
if (id != 0) //if it's zero then its not valid
image.setImageResource(id);是你想要的吗?它可能不喜欢多个文件夹,但值得一试。
https://stackoverflow.com/questions/3691295
复制相似问题