gpt4 book ai didi

android - 如果其文件路径存在,则将 uri 添加到 ListView

转载 作者:行者123 更新时间:2023-11-30 03:04:57 25 4
gpt4 key购买 nike

我的 String string_uri 中有 3 个图像 uri,我使用 for 循环将它们添加到我的 ListView 中。但我只想在它们的文件路径存在时添加它们。

ListView lv;
ArrayList<Uri> array_list = new ArrayList<Uri>();
ArrayAdapter<Uri> array_adapter;

Oncreate of my fragment..

array_adapter = new ArrayAdapter<Uri> (getActivity(), android.R.layout.simple_list_item_1, array_list);
lv = (ListView) v.findViewById(R.id.list_sfai);
lv.setAdapter(array_adapter);

String string_uri = "content://media/external/images/media/5042, content://media/external/images/media/5043, content://media/external/images/media/5044";

for(String image_uri : string_uri.split(", ")) {

Cursor c = getActivity().getContentResolver().query(Uri.parse(image_uri),null,null,null,null);
c.moveToNext();
String path = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
c.close();
File filePath = new File(path);

if (filePath.exists()) {

Uri image_Uri = Uri.parse(image_uri);
array_list.add(image_Uri);
sfFive.onGetUri(array_list);
array_adapter.notifyDataSetChanged();
}
}

一切正常。但是,例如,如果我删除图库中 content://media/external/images/media/5042 的图像,然后打开应用程序,我的应用程序就会崩溃。这是我的 LogCat:

02-23 02:48:26.536: E/AndroidRuntime(32245): FATAL EXCEPTION: main
02-23 02:48:26.536: E/AndroidRuntime(32245): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-23 02:48:26.536: E/AndroidRuntime(32245): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
02-23 02:48:26.536: E/AndroidRuntime(32245): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-23 02:48:26.536: E/AndroidRuntime(32245): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
02-23 02:48:26.536: E/AndroidRuntime(32245): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
02-23 02:48:26.536: E/AndroidRuntime(32245): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
02-23 02:48:26.536: E/AndroidRuntime(32245): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
02-23 02:48:26.536: E/AndroidRuntime(32245): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)

最佳答案

如果游标没有任何数据,你需要应用一个条件,而不是直接在你的代码中使用 moveToNext...

 if(cursor.moveToFirst()){ // cursor.moveToNext() also can help u
//then ur code...

}

关于android - 如果其文件路径存在,则将 uri 添加到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21958821/

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