gpt4 book ai didi

java - 在 RecyclerView 中从外部存储图像文件夹获取图像?

转载 作者:行者123 更新时间:2023-12-01 18:47:31 25 4
gpt4 key购买 nike

我的外部存储中有一个文件夹,我的文件夹名称是(Whatsapp Sticker)。当我这样做时,我想访问此文件夹中的所有图像..一张图像返回到文件夹大小的长度..如何获取所有图像?

这是我获取所有图像的 Activity :

public class CupyActivity extends AppCompatActivity {
RecyclerView recyclerView;
recyclerAdapterCupy recyclerAdapterCupy;
GridLayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cupy);
recyclerView = findViewById(R.id.recycler1);
recyclerAdapterCupy = new recyclerAdapterCupy(CupyActivity.this, getData());
recyclerView.setAdapter(recyclerAdapterCupy);
layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);


}

private ArrayList<GetCupy> getData() // this Method return one image again and Again... in my folder 26 images but 1 image return 26times
{
ArrayList<GetCupy> arrayList = new ArrayList<>();
File downloadfile = Environment.getExternalStorageDirectory();
File file = new File(downloadfile + "/Whatsapp Sticker");
GetCupy getCupy = new GetCupy();
if (file.exists())
{
File[] files = file.listFiles();

for (int i = 0; i<files.length; i++)
{
File file1 = files[i];

getCupy.setName(file1.getName());
getCupy.setUri(Uri.fromFile(file1));
Log.d("abcdf", "getData: "+Uri.fromFile(file1));

arrayList.add(getCupy);
Log.d("abc", "getData: "+arrayList);
}
}
return arrayList;
}
}

最佳答案

在 for 循环内创建一个 Cupy 数据对象..

 private ArrayList<GetCupy> getData()  
{
ArrayList<GetCupy> arrayList = new ArrayList<>();

File file = new File(Environment.getExternalStorageDirectory() + "/Whatsapp Sticker");

if (file.exists())
{
File[] files = file.listFiles();

for (int i = 0; i<files.length; i++)
{
getCupy = new GetCupy();
File file1 = files[i];

getCupy.setName(file1.getName());
getCupy.setUri(Uri.fromFile(file1));
Log.d("abcdf", "getData: "+Uri.fromFile(file1));

arrayList.add(getCupy);
Log.d("abc", "getData: "+arrayList);
}
}
return arrayList;

}

关于java - 在 RecyclerView 中从外部存储图像文件夹获取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59800350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com