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:Main.java

static String getFileName(Context context, Uri uri) {
    String result = null;//from ww  w  . j a  va2 s  .c o  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));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf(File.separator);
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:Main.java

public static String uriToString(Context context, Uri uri) {
    String scheme = uri.getScheme();
    if (scheme != null) {
        if (scheme.equals("http") || scheme.equals("https")) {
            return uri.toString();
        } else if (scheme.equals("content") || scheme.equals("file")) {
            Cursor cursor = context.getContentResolver().query(uri,
                    new String[] { OpenableColumns.DISPLAY_NAME }, null, null, null);
            if (cursor.moveToNext()) {
                String name = cursor.getString(0);
                cursor.close();//from  w  ww  . ja v  a 2  s  .c o  m
                return name;
            }
            cursor.close();
            return uri.getPath();
        }
    }
    return uri.toString();
}

From source file:com.allen.mediautil.ImageTakerHelper.java

/**
 * ?//w  w w.j  a  va  2 s  .com
 *
 * @param data 
 * @return ?
 */
public static String readBitmapFromAlbumResult(Context context, Intent data) {
    Uri imageUri = data.getData();
    if (imageUri == null) {
        return null;
    } else if (imageUri.toString().startsWith("file:///")) {
        try {
            // ??
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(imageUri.getPath(), options);
            return imageUri.getPath();
        } catch (Exception ex) {
            Toast.makeText(context.getApplicationContext(),
                    "?", Toast.LENGTH_SHORT).show();
            return null;
        }
    } else {
        String[] projection = { MediaStore.MediaColumns.DATA };
        CursorLoader cursorLoader = new CursorLoader(context, imageUri, projection, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        String selectedImagePath = cursor.getString(column_index);
        cursor.close();
        return selectedImagePath;
    }
}

From source file:com.andrewshu.android.reddit.common.Common.java

/**
 * //w  w  w. j  av  a2s  .  com
 * @param url
 * @param context
 * @param requireNewTask set this to true if context is not an Activity
 * @param bypassParser
 * @param useExternalBrowser
 */
public static void launchBrowser(Context context, String url, String threadUrl, boolean requireNewTask,
        boolean bypassParser, boolean useExternalBrowser, boolean saveHistory) {

    try {
        if (saveHistory) {
            Browser.updateVisitedHistory(context.getContentResolver(), url, true);
        }
    } catch (Exception ex) {
        if (Constants.LOGGING)
            Log.i(TAG, "Browser.updateVisitedHistory error", ex);
    }

    Uri uri = Uri.parse(url);

    if (!bypassParser) {
        if (Util.isRedditUri(uri)) {
            String path = uri.getPath();
            Matcher matcher = COMMENT_LINK.matcher(path);
            if (matcher.matches()) {
                if (matcher.group(3) != null || matcher.group(2) != null) {
                    CacheInfo.invalidateCachedThread(context);
                    Intent intent = new Intent(context, CommentsListActivity.class);
                    intent.setData(uri);
                    intent.putExtra(Constants.EXTRA_NUM_COMMENTS, Constants.DEFAULT_COMMENT_DOWNLOAD_LIMIT);
                    if (requireNewTask)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                    return;
                }
            }
            matcher = REDDIT_LINK.matcher(path);
            if (matcher.matches()) {
                CacheInfo.invalidateCachedSubreddit(context);
                Intent intent = new Intent(context, ThreadsListActivity.class);
                intent.setData(uri);
                if (requireNewTask)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                return;
            }
            matcher = USER_LINK.matcher(path);
            if (matcher.matches()) {
                Intent intent = new Intent(context, ProfileActivity.class);
                intent.setData(uri);
                if (requireNewTask)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                return;
            }
        } else if (Util.isRedditShortenedUri(uri)) {
            String path = uri.getPath();
            if (path.equals("") || path.equals("/")) {
                CacheInfo.invalidateCachedSubreddit(context);
                Intent intent = new Intent(context, ThreadsListActivity.class);
                intent.setData(uri);
                if (requireNewTask)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else {
                // Assume it points to a thread aka CommentsList
                CacheInfo.invalidateCachedThread(context);
                Intent intent = new Intent(context, CommentsListActivity.class);
                intent.setData(uri);
                intent.putExtra(Constants.EXTRA_NUM_COMMENTS, Constants.DEFAULT_COMMENT_DOWNLOAD_LIMIT);
                if (requireNewTask)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
            return;
        }
    }
    uri = Util.optimizeMobileUri(uri);

    // Some URLs should always be opened externally, if BrowserActivity doesn't support their content.
    if (Util.isYoutubeUri(uri) || Util.isAndroidMarketUri(uri))
        useExternalBrowser = true;

    if (useExternalBrowser) {
        Intent browser = new Intent(Intent.ACTION_VIEW, uri);
        browser.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
        if (requireNewTask)
            browser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(browser);
    } else {
        Intent browser = new Intent(context, BrowserActivity.class);
        browser.setData(uri);
        if (threadUrl != null)
            browser.putExtra(Constants.EXTRA_THREAD_URL, threadUrl);
        if (requireNewTask)
            browser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(browser);
    }
}

From source file:Main.java

/**
 * Return file size from Uri/*from   w  w w  .j a v a 2  s .c o  m*/
 *
 * @param uri file URI
 * @return return file size
 */
public static long getFileSizeFromUri(Context context, Uri uri) {
    long size = 0;
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
                size = cursor.getInt(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        size = file.length();
    }
    return size;
}

From source file:Main.java

public static String encodeUrl(String url) {
    Uri uri = Uri.parse(url);

    try {//from   ww w  .j  a  v  a 2 s.  c  om
        Map<String, List<String>> splitQuery = splitQuery(uri);
        StringBuilder encodedQuery = new StringBuilder();
        for (String key : splitQuery.keySet()) {
            for (String value : splitQuery.get(key)) {
                if (encodedQuery.length() > 0) {
                    encodedQuery.append("&");
                }
                encodedQuery.append(key + "=" + URLEncoder.encode(value, "UTF-8"));
            }
        }
        String queryString = encodedQuery != null && encodedQuery.length() > 0 ? "?" + encodedQuery : "";

        URI baseUri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment());
        return baseUri + queryString;
    } catch (UnsupportedEncodingException ignore) {
    } catch (URISyntaxException ignore) {
    }

    return uri.toString();
}

From source file:Main.java

/**
 * Return file display name from Uri/*  w w  w  .  j a v  a  2s  .co m*/
 *
 * @param context Context
 * @param uri URI of the file
 * @return return file display name
 */
public static String getFileDisplayNameFromUri(Context context, Uri uri) {
    String displayName = "";
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
                displayName = cursor.getString(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        displayName = file.getName();
    }
    return displayName;
}

From source file:com.google.android.gm.ay.java

public static boolean g(final Context context, final Uri uri) {
    final String a = c.a(context.getContentResolver(), "gmail-ad-youtube-partial-authority", "youtube.com");
    final String a2 = c.a(context.getContentResolver(), "gmail-ad-youtube-path", "/watch");
    final String authority = uri.getAuthority();
    final String path = uri.getPath();
    return authority != null && path != null && authority.endsWith(a) && a2.equals(path);
}

From source file:com.cyberocw.habittodosecretary.file.StorageHelper.java

/**
 * Creates a fiile to be used as attachment.
 *//* ww  w.j  a va2  s  .co  m*/
public static FileVO createAttachmentFromUri(Context mContext, Uri uri, boolean moveSource) {
    String name = FileHelper.getNameFromUri(mContext, uri);
    String extension = FileHelper.getFileExtension(name).toLowerCase(Locale.getDefault());
    File f;
    if (moveSource) {
        f = createNewAttachmentFile(mContext, extension);
        try {
            FileUtils.moveFile(new File(uri.getPath()), f);
        } catch (IOException e) {
            //Log.e(Constants.TAG, "Can't move file " + uri.getPath());
        }
    } else {
        //getExternalFilesDir   ? ? 
        f = StorageHelper.createExternalStoragePrivateFile(mContext, uri, extension);
    }
    FileVO mAttachment = null;
    if (f != null) {
        mAttachment = new FileVO(Uri.fromFile(f), StorageHelper.getMimeTypeInternal(mContext, uri));
        mAttachment.setName(name);
        mAttachment.setSize(f.length());
    }
    return mAttachment;
}

From source file:info.papdt.blacklight.support.Utility.java

/** Create a file Uri for saving an image*/
public static Uri getOutputMediaFileUri() {
    Uri uri = Uri.fromFile(getOutputImageFile());
    lastPicPath = uri.getPath();
    return uri;//from  w  w w . j a  v a  2  s . c  o m
}