1

I'm saving my images to a folder in sdcard, now in my android app, I want to create image picker that opens this specific folder and allow picking images.

Tried many SO solutions but none worked for me!

Any help would be appreciated. Thanks in advance.

pratiti-systematix
  • 792
  • 11
  • 28

3 Answers3

0
Try the following code :
private void loadImageFromMyFolder() {

    asyncBar.setVisibility(View.GONE);
    File yourDir = new File("/rootPath/myImage/");
    yourDir.mkdir();
    ArrayList<GalleryHelper> dataT = null;
    dataT = new ArrayList<GalleryHelper>();
    videoLoder.cancel(true);
    asyncBar.setVisibility(View.GONE);
    for (File f : yourDir.listFiles()) {
        if (f.isFile()) {
            imageLoader.displayImage("/rootPath/myImage/" + f.getName(), imgSinglePick);

            GalleryHelper item = new GalleryHelper();
            item.sdcardPath = "/rootPath/myImage/" + f.getName();

            dataT.add(item);
        }
    }
    imgAdapter.addAll(dataT);
}

public class GalleryHelper {

public String sdcardPath;

public File file;

public boolean isSeleted = false;

}

Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65
0

You can get image just create a class code is given below and use it in your project:-

public class ImagePath {

    Context context;

    public ImagePath(Context context) {
        this.context = context;
    }

    public static String getPath(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }

                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[] {
                        split[1]
                };

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context The context.
     * @param uri The Uri to query.
     * @param selection (Optional) Filter used in the query.
     * @param selectionArgs (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    public static String getDataColumn(Context context, Uri uri, String selection,
            String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }


    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }
}
Pankaj
  • 7,908
  • 6
  • 42
  • 65
0

It is easy but may be lengthy since you want to create your own picker.

Steps: 1. get file path of your required directory

val folderPath = "${Environment.getExternalStorageDirectory()}/yourDirectoryName"
  1. list all files under that directory

    var arrayList: ArrayList<File> = arrayListOf()
    if(folderPath.exists()){
        var files=folderPath.listFiles();
        if(files.length!=0){
            for(int i=0;i<files.length;i++)
            {
                var file=files[i];
                arrayList.add(file);
            }
        }
    }
    
  2. you have all the files of particular folder in arrayList. Now using recyclerView and adapter you can make your picker.

Note: if your are not sure that your folder may contain other files which you don't want to pick then..exclude them using

if(file.extension=="jpeg" || file.extension=="jpg"){
    arrayList.add(file)
}


wangsun6
  • 21
  • 8
  • 1
    Literally every answer you've posted promotes your library without disclosing your affiliation. Since this answer appears to have other information in it besides the self-promotion, I edited that out. However, please refrain from doing this in the future. – EJoshuaS - Stand with Ukraine May 03 '19 at 16:16
  • @iBug It seems like this one is salvageable through editing... I could be wrong, though. I'll mod-flag it though. – EJoshuaS - Stand with Ukraine May 03 '19 at 16:18
  • @EJoshuaS Agree. – iBug May 03 '19 at 16:19
  • I'll clear the spam flag. Other answers have been deleted. I trust your judgement on the contents of that one after the edit. – Jean-François Fabre May 03 '19 at 16:20
  • @EJoshuaS i don't understand whats wrong to tell about your library when its helping others time and reducing their code. ? and about affiliation..i am sorry i don't have much knowledge on this formalities. I did what i thought is good. and at the same time i sure..i am also providing other alternatives.. When we are helping others by spending time on stackflow..searching their question and answering them..what is wrong in promoting my library? that too its free library..in which i spent weeks so that others developers can take advantage of this library. – wangsun6 May 04 '19 at 13:16
  • @wangsun6 I don't need to justify it to you - those are the site rules. – EJoshuaS - Stand with Ukraine May 06 '19 at 12:46