Example usage for android.net Uri getPath

List of usage examples for android.net Uri getPath

Introduction

In this page you can find the example usage for android.net Uri getPath.

Prototype

@Nullable
public abstract String getPath();

Source Link

Document

Gets the decoded path.

Usage

From source file:com.github.chenxiaolong.dualbootpatcher.FileUtils.java

@NonNull
public static DocumentFile getDocumentFile(@NonNull Context context, @NonNull Uri uri) {
    DocumentFile df = null;//ww w . ja  v  a  2s . c  o m

    if (FILE_SCHEME.equals(uri.getScheme())) {
        df = DocumentFile.fromFile(new File(uri.getPath()));
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && SAF_SCHEME.equals(uri.getScheme())
            && SAF_AUTHORITY.equals(uri.getAuthority())) {
        if (DocumentsContract.isDocumentUri(context, uri)) {
            df = DocumentFile.fromSingleUri(context, uri);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && DocumentsContract.isTreeUri(uri)) {
            df = DocumentFile.fromTreeUri(context, uri);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Best guess is that it's a tree...
            df = DocumentFile.fromTreeUri(context, uri);
        }
    }

    if (df == null) {
        throw new IllegalArgumentException("Invalid URI: " + uri);
    }

    return df;
}

From source file:com.hivewallet.androidclient.wallet.AddressBookProvider.java

/**
 * Resizes the provided bitmap and stores it in private storage, returning a URI to it.
 * It will be deleted on the next clean up cycle, unless marked as permanent in the mean time.  */
public static Uri storeBitmap(@Nonnull Context context, @Nonnull Bitmap bitmap) {
    if (bitmap == null)
        return null;

    Bitmap bitmapScaled = ensureReasonableSize(bitmap);
    if (bitmapScaled == null)
        return null;

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    bitmapScaled.compress(CompressFormat.PNG, 100, outStream);
    byte[] photoScaled = outStream.toByteArray();

    Uri photoUri = null;//  ww w  .j  ava2  s .  c  o  m
    try {
        /* for some reason calling DigestUtils.sha1Hex() does not work on Android */
        String hash = new String(Hex.encodeHex(DigestUtils.sha1(photoScaled)));

        /* create photo asset and database entry */
        File dir = context.getDir(Constants.PHOTO_ASSETS_FOLDER, Context.MODE_PRIVATE);
        File photoAsset = new File(dir, hash + ".png");
        photoUri = Uri.fromFile(photoAsset);
        boolean alreadyPresent = AddressBookProvider.insertOrUpdatePhotoUri(context, photoUri);
        if (!alreadyPresent) {
            FileUtils.writeByteArrayToFile(photoAsset, photoScaled);
            log.info("Saved photo asset with uri {}", photoUri);
        }

        /* use opportunity to clean up photo assets */
        List<Uri> stalePhotoUris = AddressBookProvider.deleteStalePhotoAssets(context);
        for (Uri stalePhotoUri : stalePhotoUris) {
            File stalePhotoAsset = new File(stalePhotoUri.getPath());
            FileUtils.deleteQuietly(stalePhotoAsset);
            log.info("Deleting stale photo asset with uri {}", stalePhotoUri);
        }
    } catch (IOException ignored) {
    }

    return photoUri;
}

From source file:Main.java

private static int getImageRotationAngle(Uri imageUri, ContentResolver contentResolver) throws IOException {
    int angle = 0;
    Cursor cursor = contentResolver.query(imageUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
            null, null, null);// w  ww. j a v  a 2  s.c  om
    if (cursor != null) {
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();
            angle = cursor.getInt(0);
        }
        cursor.close();
    } else {
        ExifInterface exif = new ExifInterface(imageUri.getPath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            angle = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            angle = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            angle = 90;
            break;
        default:
            break;
        }
    }
    return angle;
}

From source file:com.commonsware.android.diceware.PassphraseFragment.java

private static DocumentFileCompat buildDocFileForUri(Context ctxt, Uri document) {
    DocumentFileCompat docFile;/*w  w  w  . j a va 2s. com*/

    if (document.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        docFile = DocumentFileCompat.fromSingleUri(ctxt, document);
    } else {
        docFile = DocumentFileCompat.fromFile(new File(document.getPath()));
    }

    return (docFile);
}

From source file:Main.java

public static String getFileName(Uri uri, Context context) {
    String result = null;//w w  w.j a va 2s .c  om
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:de.wikilab.android.friendica01.Max.java

public static String getRealPathFromURI(Context ctx, Uri contentUri) {
    Log.i("FileTypes", "URI = " + contentUri + ", scheme = " + contentUri.getScheme());
    if (contentUri.getScheme().equals("file")) {
        return contentUri.getPath();
    } else {//from  w  w  w. j av  a  2 s  .  c  o  m
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String res = cursor.getString(column_index);
        cursor.close();
        return res;
    }
}

From source file:Main.java

public static String getPath(Context context, Uri uri) {

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;/*from  w  ww.j ava2  s  .  c  o m*/

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {

        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

private static String getFileName(Context context, Uri uri) {
    Log.d("suka", uri.getScheme() + " : " + context.getContentResolver().getType(uri));
    String result = null;/*from w ww  . j  a v a 2s  . co  m*/
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        Log.d("suka", "res " + uri.getPath());
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:com.jefftharris.passwdsafe.NotificationMgr.java

/** Return whether notifications are supported for the URI */
public static boolean notifSupported(PasswdFileUri uri) {
    if (uri == null) {
        return false;
    }//w  ww  .ja  v  a2s .  co  m

    switch (uri.getType()) {
    case FILE: {
        Uri fileUri = uri.getUri();
        String path = fileUri.getPath();
        return (!path.contains("/data/com.google.android.apps.")
                && !path.contains("/data/com.dropbox.android"));
    }
    case SYNC_PROVIDER: {
        return true;
    }
    case EMAIL:
    case GENERIC_PROVIDER: {
        return false;
    }
    }
    return false;
}

From source file:Main.java

public static String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;//  www  .j  av a  2  s. c  om

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}