我很困惑,下面发布的这段代码是否可以从手机的内部存储器+外部SD卡存储器中检索所有图像?

如果没有,那么如何从手机的内部存储器+外部SD卡存储器中获取所有图像?

 final String[] columns = { MediaStore.Images.Media.DATA,
 MediaStore.Images.Media._ID };
 final String orderBy = MediaStore.Images.Media._ID;

 //Stores all the images from the gallery in Cursor
 Cursor cursor = getContentResolver().query(
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
         null, orderBy);

 //Total number of images
 int count = cursor.getCount();

 //Create an array to store path to all the images
 String[] arrPath = new String[count];

 for (int i = 0; i < count; i++) {
     cursor.moveToPosition(i);
     int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
     //Store the path of the image
     arrPath[i]= cursor.getString(dataColumnIndex);
     Log.i("PATH", arrPath[i]);
 }

最佳答案

尝试这个 -

public ArrayList<String> getFilePaths()
{


    Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {MediaStore.Images.ImageColumns.DATA};
    Cursor c = null;
    SortedSet<String> dirList = new TreeSet<String>();
    ArrayList<String> resultIAV = new ArrayList<String>();

    String[] directories = null;
    if (u != null)
    {
        c = managedQuery(u, projection, null, null, null);
    }

    if ((c != null) && (c.moveToFirst()))
    {
        do
        {
            String tempDir = c.getString(0);
            tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
            try{
                dirList.add(tempDir);
            }
            catch(Exception e)
            {

            }
        }
        while (c.moveToNext());
        directories = new String[dirList.size()];
        dirList.toArray(directories);

    }

    for(int i=0;i<dirList.size();i++)
    {
        File imageDir = new File(directories[i]);
        File[] imageList = imageDir.listFiles();
        if(imageList == null)
            continue;
        for (File imagePath : imageList) {
            try {

                if(imagePath.isDirectory())
                {
                    imageList = imagePath.listFiles();

                }
                if ( imagePath.getName().contains(".jpg")|| imagePath.getName().contains(".JPG")
                        || imagePath.getName().contains(".jpeg")|| imagePath.getName().contains(".JPEG")
                        || imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG"))
                {



                    String path= imagePath.getAbsolutePath();
                    resultIAV.add(path);

                }
            }
            //  }
            catch (Exception e) {
                e.printStackTrace();
                FirebaseCrash.report(e);
            }
        }
    }

    return resultIAV;

}

10-05 18:26