如何从android

时间:2017-11-15 04:01:14

标签: android android-contentresolver

我可以从设备中的任何指定的非隐藏文件夹中获取所有图像,但是如何从隐藏的指定文件夹中获取所有图像。 一旦我在查询中提到我的隐藏文件夹名称,游标就会返回null

  public static List<MediaData> getAppScannedImages(Context context) {
    Cursor imagecursor = null;
    List<MediaData> gallerydata = new ArrayList<MediaData>();

    try {
        final String orderBy = Images.ImageColumns.DATE_TAKEN + " DESC";
        imagecursor = context.getContentResolver()
                .query(Images.Media.EXTERNAL_CONTENT_URI,
                        projectionImage,
                        Images.Media.BUCKET_DISPLAY_NAME + "='"
                                + ".myHiddenFolder" + "'", null,
                        orderBy);
        if (imagecursor != null) {
            imagecursor.moveToFirst();
            int count = imagecursor.getCount();
            for (int i = 0; i < count; i++) {
                MediaData galData = new MediaData();
                galData.setKey_id(i);
                galData.setId(imagecursor.getString(0));
                galData.setName(imagecursor.getString(1));
                galData.setPath(imagecursor.getString(2));
                galData.setDate(imagecursor.getString(3));
                gallerydata.add(galData);
                imagecursor.moveToNext();
            }
        }


    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (imagecursor != null) {
            imagecursor.close();
        }
    }
    return gallerydata;
}

3 个答案:

答案 0 :(得分:0)

您可以尝试不同的方法。 您必须从SD卡中找出隐藏文件夹列表,并在所有这些文件夹中搜索图像。

以下代码显示隐藏文件:

public void goTODir(File dir) {

//dir is initail dir like="/mnt/sdcard"
    String imageType = ".jpg";

    File[] listFile = dir.listFiles();

    if (listFile != null) {
        for (int i = 0; i < listFile.length; i++) {

            if (listFile[i].isDirectory()) {
                goTODir(listFile[i]);
            } else {
              if (listFile[i].isHidden()){
                        if(listFile[i].getName().endsWith(imageType))
                            {
                              //add to your array list
                               }


              }
            }
        }
    }    
 }

答案 1 :(得分:0)

String path = Environment.getExternalStorageDirectory().toString();
        File dir = new File(path);
          File listFile[] = dir.listFiles();
          for (int i = 0; i < listFile.length; i++) {
              if(listFile[i].getAbsolutePath().contains("your hidden folder name")){                
                  File dirtest = new File(listFile[i].getAbsolutePath()); 
                  File listFiletest[] = dirtest.listFiles();
                  for (int j = 0; j < listFiletest.length; j++) {
                     get all images from hidden folder
                  }
              }
          }

答案 2 :(得分:0)

对于Kotlin Lover

   companion object {
    const val FOLDER_PATH = "/YourFolder/.hideen/"
}

/**
 * Method to get all Image Path
 * @return [ArrayList]
 * */
fun getImagePath(): ArrayList<String> {

    // image path list
    val list: ArrayList<String> = ArrayList()

    // fetching file path from storage
    val file = File(Environment.getExternalStorageDirectory().toString() + FOLDER_PATH)
    val listFile = file.listFiles()

    if (listFile != null && listFile.isNullOrEmpty()) {
        Arrays.sort(listFile, LastModifiedFileComparator.LASTMODIFIED_REVERSE)
    }

    if (listFile != null) {
        for (imgFile in listFile) {
            if (
                imgFile.name.endsWith(".jpg")
                || imgFile.name.endsWith(".jpeg")
                || imgFile.name.endsWith(".png")
            ) {
                val model : String = imgFile.absolutePath
                list.add(model)
            }
        }
    }

    // return imgPath List
    return list
}