Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FileUtils {
- public static boolean isLocal(String url) {
- if (url != null && !url.startsWith("http://") && !url.startsWith("https://")) {
- return true;
- }
- return false;
- }
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is ExternalStorageProvider.
- * @author paulburke
- */
- 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.
- * @author paulburke
- */
- 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.
- * @author paulburke
- */
- public 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 Old Google Photos.
- */
- public static boolean isGoogleOldPhotosUri(Uri uri) {
- return "com.google.android.apps.photos.content".equals(uri.getAuthority());
- }
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is New Google Photos.
- */
- public static boolean isGoogleNewPhotosUri(Uri uri) {
- return "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority());
- }
- 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;
- }
- public static String getPath(final Context context, final Uri uri) {
- // DocumentProvider
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- if(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;
- }
- 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 the remote address
- if (isGoogleOldPhotosUri(uri)) {
- // return http path, then download file.
- return uri.getLastPathSegment();
- }
- else if (isGoogleNewPhotosUri(uri)) {
- if(getDataColumn(context, uri, null, null) == null) {
- return getDataColumn(context, Uri.parse(getImageUrlWithAuthority(context,uri)), null, null);
- }else{
- return getDataColumn(context, uri, null, null);
- }
- }
- return getDataColumn(context, uri, null, null);
- }
- // File
- else if ("file".equalsIgnoreCase(uri.getScheme())) {
- return uri.getPath();
- }
- }else{
- String[] proj = { MediaStore.Images.Media.DATA };
- String result = null;
- CursorLoader cursorLoader = new CursorLoader(
- context,
- uri, proj, null, null, null);
- Cursor cursor = cursorLoader.loadInBackground();
- if(cursor != null){
- int column_index =
- cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- result = cursor.getString(column_index);
- }
- return result;
- }
- return null;
- }
- public static File getFile(Context context, Uri uri) {
- if (uri != null) {
- String path = getPath(context, uri);
- if (path != null && isLocal(path)) {
- return new File(path);
- }
- }
- return null;
- }
- public static String getImageUrlWithAuthority(Context context, Uri uri) {
- InputStream is = null;
- if (uri.getAuthority() != null) {
- try {
- is = context.getContentResolver().openInputStream(uri);
- Bitmap bmp = BitmapFactory.decodeStream(is);
- return writeToTempImageAndGetPathUri(context, bmp).toString();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }finally {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return null;
- }
- public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
- String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
- return Uri.parse(path);
- }
- }
- File files = FileUtils.getFile(context, uri);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement