Pra*_*tik 22 android image gallery
由于我支持我的应用程序到Kitkat版本,现在在这里从库中检索文件的方式是不同的.
我更喜欢KitKat上的这个Android Gallery为Intent.ACTION_GET_CONTENT返回不同的Uri,用于从库中检索文件并成功工作但是我需要该文件的绝对路径,我得到了
content://com.android.providers.media.documents/document/image:2505
Run Code Online (Sandbox Code Playgroud)
对于19以下版本我们使用uri不同使用我这样得到路径
Cursor cursor = this.getContentResolver().query(originalUri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String fpath = cursor.getString(column_index);
Run Code Online (Sandbox Code Playgroud)
但是在19版本中它会给我null值如何获得用户选择的图像文件的绝对路径.
谢谢
Pra*_*tik 27
这是选择文件后访问绝对路径的一种方法.
以这种方式获取KK(KitKat)的新URI格式的数据之后
content://com.android.providers.media.documents/document/image:2505
Run Code Online (Sandbox Code Playgroud)
只需提取文档的ID即可
if(requestCode == GALLERY_KITKAT_INTENT_CALLED && resultCode == RESULT_OK){
Uri originalUri = data.getData();
final int takeFlags = data.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
/* now extract ID from Uri path using getLastPathSegment() and then split with ":"
then call get Uri to for Internal storage or External storage for media I have used getUri()
*/
String id = originalUri.getLastPathSegment().split(":")[1];
final String[] imageColumns = {MediaStore.Images.Media.DATA };
final String imageOrderBy = null;
Uri uri = getUri();
String selectedImagePath = "path";
Cursor imageCursor = managedQuery(uri, imageColumns,
MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
if (imageCursor.moveToFirst()) {
selectedImagePath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
Log.e("path",selectedImagePath ); // use selectedImagePath
}else if() {
// for older version use existing code here
}
// By using this method get the Uri of Internal/External Storage for Media
private Uri getUri() {
String state = Environment.getExternalStorageState();
if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26631 次 |
| 最近记录: |