簡體   English   中英

僅獲取圖庫文件夾:Intent.ACTION_GET_CONTENT

[英]Get only gallery folder : Intent.ACTION_GET_CONTENT

我想實現一個圖片選擇器,因為從這兩個文件夾中選擇一個圖像時,Android應用程序在“ 最近的圖像 文件夾 ”和“ 下載文件夾 ”存在錯誤時崩潰了,我想將它們隱藏起來,或者直接打開“圖庫”文件夾而不顯示文件夾選擇器面板,

這是我的實際代碼:

Intent intent = new Intent (Intent.ACTION_GET_CONTENT);
startActivityForResult (intent, 1000);

在on Result方法中:

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent intent) {

    super.onActivityResult (requestCode, resultCode, intent);

    String uri = intent.getDataString ();
    String absolutePath = getRealPathFromURI (uri);
    String compressedFilePath = compressImage (absolutePath);

    encodedResult = base64File (new File (compressedFilePath)); //String
}

protected String getRealPathFromURI (String contentURI, UIView uiView) {
    Uri contentUri = Uri.parse(contentURI);
    Cursor cursor = ((ContextWrapper) uiView).getContentResolver ().query(contentUri, null, null, null, null);
    if (cursor == null) {
        return contentUri.getPath();
    } else {
        cursor.moveToFirst();
        int index = cursor.getColumnIndex(MediaColumns.DATA);
        return cursor.getString(index);
    }
}

因此,當我從諸如Gallery,Pictures等文件夾中選擇圖像時,它工作得很好。 但是對於2個文件夾: 下載最近崩潰,它給了我:

java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

也許有一種我不知道的更正確的方法?

您對此有任何想法嗎? 我該如何實現? 謝謝。

它崩潰了,給了我

那是因為您假設任何Uri都可以轉換為File 從來沒有得到過支持。

使用ContentResolver並通過openInputStream() (或openOutputStream() ,如果適用)使用Uri

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM