简体   繁体   中英

How to get Image and Image Name from the file?

I set below codes for fragment tab. I am trying to get image and it's name from the file and adding in separate arraylists. I am getting images successfully but failed to get respective names. Everytime cursor is being null. What can I do?

 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();
        }
    }

my plan is to create gallery of images showing respective names below it.

you can get from path

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM