gpt4 book ai didi

java - 显示特定目录中的所有图像

转载 作者:行者123 更新时间:2023-12-01 22:31:41 26 4
gpt4 key购买 nike

我是安卓新手。尝试创建一个应用程序,允许用户下载图片,然后将其存储在外部存储中,我的代码工作正常,但它显示了外部存储的所有图像。我将这些图片存储在设备上的特定文件夹中,但我不知道如何显示该文件夹中所有图片的列表。

like /storage/emulated/0/due/image/ only show this location pictures.

If I write an image path, the application crashes.

我的代码

public class mainActivity extends AppCompatActivity  {
GridView gridView;
ArrayList<File> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




checkPermissions();

list = getImageFiles(new File(Environment.getExternalStorageDirectory().getPath() + " /storage/emulated/0/due/image/"));

gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new GridAdapter());

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// startActivity(new Intent(getApplicationContext(), ViewImage.class).putExtra("img", list.get(i).toString()));
}
});

}

class GridAdapter extends BaseAdapter {

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int i) {
return list.get(i);
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.image_loader_list_item, viewGroup, false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
//imageview.setImageURI(Uri.parse(getItem(i).toString()));

//Loading image from below url into imageView
Glide.with(getApplicationContext())
.load(getItem(i).toString())
.into(imageView);
return view;
}
}

ArrayList<File> getImageFiles(File root) {
ArrayList<File> list = new ArrayList();
File[] files = root.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].isDirectory()) {
list.addAll(getImageFiles(files[i]));
} else {
if(files[i].getName().endsWith(".jpg") || files[i].getName().endsWith(".png") || files[i].getName().endsWith(".gif")) {
list.add(files[i]);
}
}
}
return list;
}

private void checkPermissions(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
//Requesting permission.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
}
}

@Override //Override from ActivityCompat.OnRequestPermissionsResultCallback Interface
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted
}
return;
}
}
}

private ActivityManager.MemoryInfo getMemoryInfo() {
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo;
}


}

日志猫

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dueaoinfo.dueao_job/com.dueaoinfo.dueao_job.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.dueaoinfo.dueao_job.MainActivity.getImageFiles(MainActivity.java:91)
at com.dueaoinfo.dueao_job.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:7224)
at android.app.Activity.performCreate(Activity.java:7213)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)

最佳答案

您的代码:

list = getImageFiles(Environment.getExternalStorageDirectory());

可以替换为:

  list = getImageFiles(Environment.getExternalStorageDirectory(),"/storage/emulated/0/due/image/");

使其指向您想要选取的文件夹。

引用文献:Environment.getExternalStorageDirectory()

希望这有帮助。

关于java - 显示特定目录中的所有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58546827/

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