Anh chị cho em hỏi với ạ!
Hiện tại em có 1 list ảnh ở trong file. Nếu như làm bình thường thì mỗi lần đọc ảnh phải là R.drawable.(Tên ảnh).
Nhưng do số lượng quá nhiều nếu đọc như vậy sẽ không hay lắm. Anh chị nào có cách gì chỉ em với ạ! Em cảm ơn!
Cách đọc file ảnh trong Android
Sử dụng reflection chắc được:
R.drawable.class.getFields
//example:
Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
try {
Log.i("LOG", "R.drawable." + f.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
ref: http://stackoverflow.com/questions/13266774/how-to-get-list-of-image-with-r-drawable
3 Likes
A có thể nói rõ hơn cho em được không ạ? E vẫn không hiểu mấy ạ
ý là như vầy:
final R.drawable drawableResources = new R.drawable();
final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();
for (int i=0; i < fields.length;i++)
{
resourceId[i] = fields[i].getInt(drawableResources);
/* till here you get all the images into the int array resourceId.*/
}
imageview= (ImageView)findViewById(R.id.imageView);
if(your condition)
{
imageview.setImageResource(resourceId[any]);
}
1 Like
Em làm được rồi ạ. Cảm ơn anh 
Làm được rồi thì show solution lên đi em, người khác bị như em còn biết chứ 
1 Like
Nó giống cái trên của anh ạ. Hiện tại code của em nó chỉ tìm ảnh mà chỉ có độ dài 1 ak 
public void findIdAlphabets(){
Field[] drawables =R.drawable.class.getDeclaredFields();
for (int i=0;i<drawables.length;i++) {
try {
Log.d("error",drawables[i].getName());
if(drawables[i].getName().length()==1){
list.add(drawables[i].getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?