我正在尝试从asset
文件夹加载图像,然后将其设置为ImageView
。我知道如果使用R.id.*
会更好,但前提是我不知道图像的id。基本上,我正在尝试通过文件名动态加载图像。
例如,我随机从数据库
中检索表示牛的元素,现在我的应用程序将显示一个'cow'图像通过ImageView
。这也适用于数据库
中的所有元素。(假设每个元素都有一个对应的图像)
如何从asset
文件夹加载图像?
看一下这个代码。在这个教程中,您可以了解如何从资产文件夹加载图像。
// 加载图像
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);
ims .close();
}
catch(IOException ex)
{
return;
}
给你:
public Bitmap getBitmapFromAssets(String fileName) throws IOException {
AssetManager assetManager = getAssets();
InputStream istr = assetManager.open(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
istr.close();
return bitmap;
}
如果您在代码中知道文件名,调用这个函数不会成为问题:
ImageView iw= (ImageView)findViewById(R.id.imageView1);
int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
iw.setImageResource(resID);
你的文件名将与drawableName相同,因此你不必处理资产。
有些答案可能会回答这个问题,但我从来不喜欢它们中的任何一个,所以最终我写了这篇文章,希望能对社区有所帮助。
从资产中获取 Bitmap
:
public Bitmap loadBitmapFromAssets(Context context, String path)
{
InputStream stream = null;
try
{
stream = context.getAssets().open(path);
return BitmapFactory.decodeStream(stream);
}
catch (Exception ignored) {} finally
{
try
{
if(stream != null)
{
stream.close();
}
} catch (Exception ignored) {}
}
return null;
}
从资产中获取Drawable
:
public Drawable loadDrawableFromAssets(Context context, String path)
{
InputStream stream = null;
try
{
stream = context.getAssets().open(path);
return Drawable.createFromStream(stream, null);
}
catch (Exception ignored) {} finally
{
try
{
if(stream != null)
{
stream.close();
}
} catch (Exception ignored) {}
}
return null;
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
示例例子:
// For a simple view:
@Override public void onCreate(Bundle savedInstanceState) {
...
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("file:///android_asset/img/fruit/cherries.jpg").into(imageView);
}
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.book,parent,false);
public static Bitmap getImageFromAssetsFile(Context mContext, String fileName) {
Bitmap image = null;
AssetManager am = mContext.getResources().getAssets();
try {
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
AssetManager assetManager = getAssets();
ImageView imageView = (ImageView) findViewById(R.id.imageView);
try (
//declaration of inputStream in try-with-resources statement will automatically close inputStream
// ==> no explicit inputStream.close() in additional block finally {...} necessary
InputStream inputStream = assetManager.open("products/product001.jpg")
) {
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmap);
} catch (IOException ex) {
//ignored
}
您可以简单地使用此 Kotlin 扩展函数,其中 String 被引用为 fileName
fun String.assetsToBitmap(context: Context): Bitmap? {
return try {
val assetManager = context.assets
val inputStream = assetManager.open(this)
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream.close()
bitmap
} catch (e: Exception) {
e.printStackTrace()
null
}
}
使用示例
val myBitmap = "sample.png".assetsToBitmap(context)
WebView web = (WebView) findViewById(R.id.webView);
web.loadUrl("file:///android_asset/pract_recommend_section1_pic2.png");
web.getSettings().setBuiltInZoomControls(true);