android讀取系統相簿返回為空的解決辦法

2021-07-09 02:43:12 字數 2603 閱讀 9957

讀取系統相簿為空的問題,今天遇到過了,找到了解決辦法,寫出來記錄一下

關於個別手機讀取到相簿為空,可能的原因是系統版本問題,4.4系統在獲取系統返回的相簿的url的時候,有可能出現為null的問題,解決辦法來自網上

/** 

* get a file path from a uri. this will get the the path for storage access

* framework documents, as well as the _data field for the mediastore and

* other file-based contentproviders.

* * @param context the context.

* @param uri the uri to query.

* @author paulburke

*/

public static string getpath(final context context, final uri uri)

// todo handle non-primary volumes

} // downloadsprovider

else if (isdownloadsdocument(uri))

// mediaprovider

else if (ismediadocument(uri)) else if ("video".equals(type)) else if ("audio".equals(type))

final string selection = "_id=?";

final string selectionargs = new string ;

return getdatacolumn(context, contenturi, selection, selectionargs);

} }

// mediastore (and general)

else if ("content".equalsignorecase(uri.getscheme()))

// file

else if ("file".equalsignorecase(uri.getscheme()))

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

try

} finally

return null;

} /**

* @param uri the uri to check.

* @return whether the uri authority is externalstorageprovider.

*/

public static boolean i***ternalstoragedocument(uri uri)

/**

* @param uri the uri to check.

* @return whether the uri authority is downloadsprovider.

*/

public static boolean isdownloadsdocument(uri uri)

/**

* @param uri the uri to check.

* @return whether the uri authority is mediaprovider.

*/

public static boolean ismediadocument(uri uri)

例子

uri uri = data.getdata();

fileinputstream fis = new fileinputstream(getpath(getbasecontext(),uri));

對獲取的到的url進行轉換,就可以避免這個問題的發生了

讀取相簿 拍照

這兩天在做專案時,做到上傳功能一塊時,碰到兩個問題,乙個是如何獲取所選的路徑,乙個是如何壓縮,在查了一些資料和看了別人寫的後總算折騰出來了,在此記錄一下。首先既然要選擇,我們就先要獲取本地所有的,android已經為我們封裝好了該意圖。12 3451intent intent newintent i...

Android呼叫系統相簿和系統相機拍照

呼叫系統相機拍照 intent new intent mediastore.action image capture startactivityforresult intent,result camara image result camara image activity的標誌。自已定義 呼叫系統...

android 相簿實時更新

於是只有問度娘了。1.重新掃瞄整個儲存空間,將資料庫更新,但是這樣代價太大 2.將新的資料加到資料庫,這樣操作對資料的安全性要求很高,所以還是不推薦 3.直接掃瞄新新增的檔案 當然也可以是該檔案所在的資料夾 最後我使用了第三種方式解決了問題 如下 uri uri uri.parse file env...