-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
373 additions
and
117 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ | |
* @author Jenly <a href="mailto:[email protected]">Jenly</a> | ||
* | ||
*/ | ||
public abstract class ViewHolderRecyclerAdapter<T, V> extends HolderRecyclerAdapter<T,ViewHolder> { | ||
public abstract class ViewHolderRecyclerAdapter<T> extends HolderRecyclerAdapter<T,ViewHolder> { | ||
|
||
public ViewHolderRecyclerAdapter(Context context, List<T> listData) { | ||
super(context, listData); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
base-util/src/main/java/com/king/base/util/UriUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.king.base.util; | ||
|
||
import android.content.ContentUris; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.provider.DocumentsContract; | ||
import android.provider.MediaStore; | ||
import android.support.annotation.RequiresApi; | ||
|
||
/** | ||
* @author Jenly <a href="mailto:[email protected]">Jenly</a> | ||
*/ | ||
public class UriUtils { | ||
|
||
/** | ||
* 根据Uri获取图片的绝对路径 | ||
* | ||
* @param context 上下文对象 | ||
* @param uri | ||
* @return 如果Uri对应的图片存在, 那么返回该文件的绝对路径, 否则返回null | ||
*/ | ||
public static String getPath(Context context, Uri uri) { | ||
int sdkVersion = Build.VERSION.SDK_INT; | ||
if (sdkVersion >= 19) { // api >= 19 | ||
return getRealPathFromUriAboveApi19(context, uri); | ||
} else { // api < 19 | ||
return getRealPathFromUriBelowAPI19(context, uri); | ||
} | ||
} | ||
|
||
/** | ||
* 适配api19以下(不包括api19),根据uri获取图片的绝对路径 | ||
* | ||
* @param context 上下文对象 | ||
* @param uri 图片的Uri | ||
* @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null | ||
*/ | ||
private static String getRealPathFromUriBelowAPI19(Context context, Uri uri) { | ||
return getDataColumn(context, uri, null, null); | ||
} | ||
|
||
/** | ||
* 适配api19及以上,根据uri获取图片的绝对路径 | ||
* | ||
* @param context 上下文对象 | ||
* @param uri 图片的Uri | ||
* @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null | ||
*/ | ||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
private static String getRealPathFromUriAboveApi19(Context context, Uri uri) { | ||
String filePath = null; | ||
if (DocumentsContract.isDocumentUri(context, uri)) { | ||
// 如果是document类型的 uri, 则通过document id来进行处理 | ||
String documentId = DocumentsContract.getDocumentId(uri); | ||
if (isMediaDocument(uri)) { // MediaProvider | ||
// 使用':'分割 | ||
String id = documentId.split(":")[1]; | ||
|
||
String selection = MediaStore.Images.Media._ID + "=?"; | ||
String[] selectionArgs = {id}; | ||
filePath = getDataColumn(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs); | ||
} else if (isDownloadsDocument(uri)) { // DownloadsProvider | ||
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId)); | ||
filePath = getDataColumn(context, contentUri, null, null); | ||
} | ||
} else if ("content".equalsIgnoreCase(uri.getScheme())){ | ||
// 如果是 content 类型的 Uri | ||
filePath = getDataColumn(context, uri, null, null); | ||
} else if ("file".equals(uri.getScheme())) { | ||
// 如果是 file 类型的 Uri,直接获取图片对应的路径 | ||
filePath = uri.getPath(); | ||
} | ||
return filePath; | ||
} | ||
|
||
/** | ||
* 获取数据库表中的 _data 列,即返回Uri对应的文件路径 | ||
* @return | ||
*/ | ||
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { | ||
String path = null; | ||
|
||
String[] projection = new String[]{MediaStore.Images.Media.DATA}; | ||
Cursor cursor = null; | ||
try { | ||
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); | ||
if (cursor != null && cursor.moveToFirst()) { | ||
int columnIndex = cursor.getColumnIndexOrThrow(projection[0]); | ||
path = cursor.getString(columnIndex); | ||
} | ||
} catch (Exception e) { | ||
if (cursor != null) { | ||
cursor.close(); | ||
} | ||
} | ||
return path; | ||
} | ||
|
||
/** | ||
* @param uri the Uri to check | ||
* @return Whether the Uri authority is MediaProvider | ||
*/ | ||
private static boolean isMediaDocument(Uri uri) { | ||
return "com.android.providers.media.documents".equals(uri.getAuthority()); | ||
} | ||
|
||
/** | ||
* @param uri the Uri to check | ||
* @return Whether the Uri authority is DownloadsProvider | ||
*/ | ||
private static boolean isDownloadsDocument(Uri uri) { | ||
return "com.android.providers.downloads.documents".equals(uri.getAuthority()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.