Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android使用java代码从drawable加载图像_Java_Android - Fatal编程技术网

Android使用java代码从drawable加载图像

Android使用java代码从drawable加载图像,java,android,Java,Android,我的drawable文件夹中有1000多个图像,我需要将所有这些图像加载到一个数组列表中,以便在程序运行时进行搜索。 在android中有没有任何可能的方法可以将所有图像动态加载到ArrayList中 谢谢这段代码正在运行并经过我的测试,您可以从drawable获取所有图像并将其保存在ArrayList中 但在我看来,这是一个非常糟糕的主意 代码: ArrayList<Integer> arrayOfIcons = new ArrayList<>(); Field[]

我的drawable文件夹中有1000多个图像,我需要将所有这些图像加载到一个数组列表中,以便在程序运行时进行搜索。 在android中有没有任何可能的方法可以将所有图像动态加载到ArrayList中


谢谢

这段代码正在运行并经过我的测试,您可以从drawable获取所有图像并将其保存在ArrayList中

但在我看来,这是一个非常糟糕的主意

代码:

 ArrayList<Integer> arrayOfIcons = new ArrayList<>();

Field[] drawables = R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        int resID = getResources().getIdentifier(f.getName() , "drawable", getPackageName());
        arrayOfIcons.add(resID);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(arrayOfIcons.get(0));
ArrayList arrayOfIcons=new ArrayList();
Field[]drawables=R.drawable.class.getFields();
用于(字段f:可抽出式){
试一试{
int resID=getResources().getIdentifier(f.getName(),“drawable”,getPackageName());
阵列。添加(剩余);
}捕获(例外e){
e、 printStackTrace();
}
}
ImageView ImageView=findViewById(R.id.ImageView);
setImageResource(arrayOfIcons.get(0));

我希望它能对您有所帮助。

这段代码正在运行并经过我的测试,您可以从drawable获取所有图像并将其保存在ArrayList中

但在我看来,这是一个非常糟糕的主意

代码:

 ArrayList<Integer> arrayOfIcons = new ArrayList<>();

Field[] drawables = R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        int resID = getResources().getIdentifier(f.getName() , "drawable", getPackageName());
        arrayOfIcons.add(resID);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(arrayOfIcons.get(0));
ArrayList arrayOfIcons=new ArrayList();
Field[]drawables=R.drawable.class.getFields();
用于(字段f:可抽出式){
试一试{
int resID=getResources().getIdentifier(f.getName(),“drawable”,getPackageName());
阵列。添加(剩余);
}捕获(例外e){
e、 printStackTrace();
}
}
ImageView ImageView=findViewById(R.id.ImageView);
setImageResource(arrayOfIcons.get(0));

我希望它能帮助您。

导入java.lang.reflect.Field

Field[] ID_Fields = R.drawable.class.getFields();
int[] resArray = new int[ID_Fields.length];
for(int i = 0; i < ID_Fields.length; i++) {
    try {
        resArray[i] = ID_Fields[i].getInt(null);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Field[]ID\u Fields=R.drawable.class.getFields();
int[]resArray=新的int[ID_Fields.length];
对于(int i=0;i
导入java.lang.reflect.Field

Field[] ID_Fields = R.drawable.class.getFields();
int[] resArray = new int[ID_Fields.length];
for(int i = 0; i < ID_Fields.length; i++) {
    try {
        resArray[i] = ID_Fields[i].getInt(null);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Field[]ID\u Fields=R.drawable.class.getFields();
int[]resArray=新的int[ID_Fields.length];
对于(int i=0;i
这是最终工作代码:

 ImageView imageView = findViewById(R.id.testImage);
    //load all the icon names from drawable
    ArrayList<String> arrayOfIcons = new ArrayList<>();

    //load the array containing specific characters
    containingArray = new ArrayList<>();

    Field[] drawables = R.drawable.class.getFields();
    for (Field f : drawables) {
        try {
            int resID = getResources().getIdentifier(f.getName() , "drawable", getPackageName());
            arrayOfIcons.add(f.getName());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //search for all similar keywords
    for(int i =0; i < arrayOfIcons.size();i++){
        if(arrayOfIcons.get(i).contains(title_string) && arrayOfIcons.get(i).contains("_")){
            containingArray.add(arrayOfIcons.get(i));
        }
    }
    //split array
    for(int i = 0; i < containingArray.size();i++){

        //finding the index that contains the keyword 
        if(containingArray.get(i).substring(0,containingArray.get(i).lastIndexOf("_")).equalsIgnoreCase(title_string)){
            Context context = imageView.getContext();
            int id = context.getResources().getIdentifier(containingArray.get(i), "drawable", context.getPackageName());
            imageView.setImageResource(id);
        }
        System.out.println("ContainingArray: "+containingArray.get(i));
    }
ImageView-ImageView=findviewbyd(R.id.testImage);
//从drawable加载所有图标名称
ArrayList arrayOfIcons=新的ArrayList();
//加载包含特定字符的数组
containingArray=newArrayList();
Field[]drawables=R.drawable.class.getFields();
用于(字段f:可抽出式){
试一试{
int resID=getResources().getIdentifier(f.getName(),“drawable”,getPackageName());
add(f.getName());
}捕获(例外e){
e、 printStackTrace();
}
}
//搜索所有相似的关键字
对于(int i=0;i
这是最终工作代码:

 ImageView imageView = findViewById(R.id.testImage);
    //load all the icon names from drawable
    ArrayList<String> arrayOfIcons = new ArrayList<>();

    //load the array containing specific characters
    containingArray = new ArrayList<>();

    Field[] drawables = R.drawable.class.getFields();
    for (Field f : drawables) {
        try {
            int resID = getResources().getIdentifier(f.getName() , "drawable", getPackageName());
            arrayOfIcons.add(f.getName());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //search for all similar keywords
    for(int i =0; i < arrayOfIcons.size();i++){
        if(arrayOfIcons.get(i).contains(title_string) && arrayOfIcons.get(i).contains("_")){
            containingArray.add(arrayOfIcons.get(i));
        }
    }
    //split array
    for(int i = 0; i < containingArray.size();i++){

        //finding the index that contains the keyword 
        if(containingArray.get(i).substring(0,containingArray.get(i).lastIndexOf("_")).equalsIgnoreCase(title_string)){
            Context context = imageView.getContext();
            int id = context.getResources().getIdentifier(containingArray.get(i), "drawable", context.getPackageName());
            imageView.setImageResource(id);
        }
        System.out.println("ContainingArray: "+containingArray.get(i));
    }
ImageView-ImageView=findviewbyd(R.id.testImage);
//从drawable加载所有图标名称
ArrayList arrayOfIcons=新的ArrayList();
//加载包含特定字符的数组
containingArray=newArrayList();
Field[]drawables=R.drawable.class.getFields();
用于(字段f:可抽出式){
试一试{
int resID=getResources().getIdentifier(f.getName(),“drawable”,getPackageName());
add(f.getName());
}捕获(例外e){
e、 printStackTrace();
}
}
//搜索所有相似的关键字
对于(int i=0;i
有1000张图片,你想过APK大小吗?是的,你可以,但你不应该。哇。是的,我不会把它们储存在arraylist里。我也不会把它们放在本地。为什么?您应该考虑将它们存储在服务器中,然后用它保存缩略图,这样,如果您担心的话,它的加载速度会更快。此时,您可以调用服务器,获取图像,然后将它们存储在列表中以解析thorugh。但是,这仍然需要很长时间。你需要重新考虑你的方法。我希望你计划用图像路径或引用来填充ArrayList,而不是二进制文件。下面的答案是正确的,但我相信你有一个更大的问题。我对您的1000个图像问题的建议是使用简单的云服务(Dropbox、Firebase存储…)来存储