2015-05-12 124 views
1

我將圖像保存到sdcard中的文件夾,現在在我的android應用程序中,我想創建圖像選擇器,打開此特定文件夾並允許選取圖像。Android圖像選取器從指定的文件夾路徑中選取圖像

試過很多SO解決方案,但沒有爲我工作!

任何幫助,將不勝感激。 在此先感謝。

+0

http://stackoverflow.com/questions/17165972/android-how-to-open-a -specific-folder-via-intent-and-show-its-content-in-a-file –

回答

0

不,這是不可能的。如果必要的話,你可以實現你自己的畫廊,這將挑選圖像只能從

android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI

android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI 
+0

好的,那意味着我可以從特定的文件夾圖像創建我自己的圖庫了嗎? –

+0

您可以將圖像保存到您的自定義圖庫,然後從該圖庫中獲取這些圖像 –

+0

我已經將圖像保存到sd卡文件夾,現在如何獲取它們以構建我自己的自定義圖庫?你可以請樣品幫忙嗎? –

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

公共類GalleryHelper {

public String sdcardPath; 

public File file; 

public boolean isSeleted = false; 

}

0

你可以像剛纔創建以下類代碼,並給出在項目中使用它: -

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

如何使用它?我將如何處理這個圖像路徑? –

+0

你有沒有在你的項目中使用過任何課程? – Pankaj

+0

當然,是的.. –

相關問題