如何从文件中获取图像和图像名称?

How to get Image and Image Name from the file?

我为片段选项卡设置了以下代码。我正在尝试从文件中获取图像及其名称并添加单独的数组列表。我成功获取图像但未能获取各自的名称。每次游标为空。我能做什么?

 public void imageList()
    {
        File imagePathForImage = new File(Environment.getExternalStorageDirectory());
        imageList = imageReader(imagePathForImage);

        if(imageList.size() != 0)
        {
            gallery.setAdapter(new imageAdapater());
            gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent openImage = new Intent(getActivity(), FullImageActivity.class);
                    openImage.putExtra("Img :", imageList.get(i).toString());
                    startActivity(openImage);
                }
            });
        }
        else
        {
            Toast.makeText(getActivity(), "No image Found", Toast.LENGTH_SHORT).show();
        }
    }


    public void imageNameList ()
    {
        imageNameList = new ArrayList<String>();
        File imagePathForName = new File(Environment.getExternalStorageDirectory() );
        Cursor cursor = getContext().getContentResolver().query(Uri.fromFile(imagePathForName), null, null, null, null);
        if (cursor == null)
        {
            Toast.makeText(getActivity(), "No Image File Found", Toast.LENGTH_SHORT).show();
        } else
        {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);
            imageNameList.add(cursor.getString(idx));
            Log.i( "FineName: " , cursor.getString(idx) );
            cursor.close();
        }
    }

我的计划是创建图片库,在图片下方显示各个名称。

你可以从路径

String filename=path.substring(path.lastIndexOf("/")+1);