자산 폴더에서 이미지로드
asset폴더 에서 이미지를로드 한 다음 ImageView. 나는 이것을 사용하면 훨씬 더 좋다는 것을 알고 R.id.*있지만 전제는 이미지의 ID를 모른다는 것입니다. 기본적으로 파일 이름을 통해 이미지를 동적으로로드하려고합니다.
예를 들어, 나는 'cow'를database 나타내는 요소를 무작위로 검색합니다 . 이제 내 응용 프로그램 은를 통해 'cow' 의 이미지를 표시하는 것 입니다. 의 모든 요소에 대해서도 마찬가지입니다 . (가정은 모든 요소에 대해 동등한 이미지가 있다는 것입니다)ImageViewdatabase
미리 감사드립니다.
편집하다
질문을 잊었습니다. asset폴더 에서 이미지를 어떻게로드 합니까?
코드에서 파일 이름을 알고 있다면 이것을 호출해도 문제가되지 않습니다.
ImageView iw= (ImageView)findViewById(R.id.imageView1);
int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
iw.setImageResource(resID);
파일 이름은 drawableName과 같은 이름이므로 자산을 다룰 필요가 없습니다.
이 코드를 확인하십시오 . 이 튜토리얼에서는 자산 폴더에서 이미지를로드하는 방법을 찾을 수 있습니다.
// 이미지로드
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) {
AssetManager assetManager = getAssets();
InputStream istr = assetManager.open(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
이 답변 중 일부는 질문에 답할 수 있지만 그중 어느 것도 좋아하지 않았으므로 결국 이것을 작성하여 커뮤니티를 돕는 것입니다.
취득 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;
}
WebView web = (WebView) findViewById(R.id.webView);
web.loadUrl("file:///android_asset/pract_recommend_section1_pic2.png");
web.getSettings().setBuiltInZoomControls(true);
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;
}
Android 개발자 문서 에 따르면 비트 맵으로 로드 하면 앱 성능이 저하 될 수 있습니다 . 링크가 있습니다 ! 그래서 doc은 Glide 라이브러리 를 사용하도록 제안합니다 .
If you want to load image from assets folder then using Glide library help you alots easier.
just add dependencies to build.gradle (Module:app) from https://github.com/bumptech/glide
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
sample example :
// 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);
}
In case not worked by above method : Replace this object with view object from below code (only if you have Inflate method applied as below in your code).
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.book,parent,false);
This worked in my use case:
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
}
(see also https://javarevisited.blogspot.com/2014/10/right-way-to-close-inputstream-file-resource-in-java.html)
ReferenceURL : https://stackoverflow.com/questions/11734803/load-an-image-from-assets-folder
'Nice programing' 카테고리의 다른 글
| LibGDX (Java)에서 좌표계 변경 (0) | 2021.01.05 |
|---|---|
| std :: map에 요소가 있는지 확인하는 방법은 무엇입니까? (0) | 2020.12.31 |
| Swift의 정수 배열에서 NSIndexSet 만들기 (0) | 2020.12.31 |
| 목록이 주문되었는지 확인하는 방법은 무엇입니까? (0) | 2020.12.31 |
| Knockout JS 대신 Knockout MVC를 사용하는 이유가 있습니까? (0) | 2020.12.31 |