Example usage for android.net Uri getLastPathSegment

List of usage examples for android.net Uri getLastPathSegment

Introduction

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

Prototype

@Nullable
public abstract String getLastPathSegment();

Source Link

Document

Gets the decoded last segment in the path.

Usage

From source file:com.pavlospt.rxfile.RxFile.java

private static boolean isMediaUri(Uri uri) {
    if (uri.getAuthority().equals(Constants.MEDIA_DOCUMENTS_AUTHORITY)) {
        return uri.getLastPathSegment().contains(Constants.IMAGE)
                || uri.getLastPathSegment().contains(Constants.VIDEO);
    }//from   w w  w.j a va  2 s.  co m
    return Constants.MEDIA_AUTHORITY.equals(uri.getAuthority());
}

From source file:com.appunite.appunitevideoplayer.PlayerActivity.java

/**
 * Makes a best guess to infer the type from a media {@link Uri} and an optional overriding file
 * extension.//ww w .ja  v a2 s  .  com
 *
 * @param uri           The {@link Uri} of the media.
 * @param fileExtension An overriding file extension.
 * @return The inferred type.
 */
private static int inferContentType(Uri uri, String fileExtension) {
    String lastPathSegment = !TextUtils.isEmpty(fileExtension) ? "." + fileExtension : uri.getLastPathSegment();
    if (lastPathSegment == null) {
        return TYPE_OTHER;
    } else if (lastPathSegment.endsWith(EXT_DASH)) {
        return TYPE_DASH;
    } else if (lastPathSegment.endsWith(EXT_SS)) {
        return TYPE_SS;
    } else if (lastPathSegment.endsWith(EXT_HLS)) {
        return TYPE_HLS;
    } else {
        return TYPE_OTHER;
    }
}

From source file:com.nextgis.mobile.map.Layer.java

protected static String getFileNameByUri(final Context context, Uri uri, String defaultName) {
    String fileName = defaultName;
    Uri filePathUri = uri;
    try {//from  w  w w.java  2s .co  m
        if (uri.getScheme().toString().compareTo("content") == 0) {
            Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
            if (cursor.moveToFirst()) {
                int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                //Instead of "MediaStore.Images.Media.DATA" can be used "_data"
                filePathUri = Uri.parse(cursor.getString(column_index));
                fileName = filePathUri.getLastPathSegment().toString();
            }
        } else if (uri.getScheme().compareTo("file") == 0) {
            fileName = filePathUri.getLastPathSegment().toString();
        } else {
            fileName = fileName + "_" + filePathUri.getLastPathSegment();
        }
    } catch (Exception e) {
        //do nothing, only return default file name;
        Log.d(TAG, e.getLocalizedMessage());
    }
    return fileName;
}

From source file:com.purdue.CampusFeed.Activities.ContactDetailFragment.java

public static void updateContactIdFromUri(Uri uri) {
    ContactDetailFragment.SelectedContactID = uri.getLastPathSegment();
    ContactDetailFragment.CONTACT_PHONE_QUERY_SELECTION = Data.CONTACT_ID + "="
            + ContactDetailFragment.SelectedContactID;
}

From source file:com.nuvolect.securesuite.main.SharedMenu.java

public static void sharedOnActivityResult(Activity act, int requestCode, int resultCode, Intent data) {

    if (DEBUG)/*from  www.j  a v a  2 s . c  om*/
        LogUtil.log("SharedMenu myOnActivityResult requestCode: " + requestCode);

    switch (requestCode) {

    case CConst.IMPORT_VCARD_BROWSE_ACTION: {

        if (resultCode == RESULT_OK) {

            Bundle activityResultBundle = data.getExtras();
            String path = activityResultBundle.getString(CConst.IMPORT_VCF_PATH);

            new ImportVcardAsync().execute(path);
        }
        break;
    }
    case CConst.IMPORT_SINGLE_CONTACT_PICKER: {

        if (resultCode == RESULT_OK) {

            Uri result = data.getData();
            String id = result.getLastPathSegment();
            LogUtil.log("Cloud contact ID: " + id);
            boolean success = true;

            if (id == null || id.isEmpty())
                success = false;
            else {

                long cloud_contact_id = Long.valueOf(id);
                success = ImportContacts.importSingleContact(m_act, cloud_contact_id);
            }
            if (!success)
                Toast.makeText(m_act, "Contact import error", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(m_act, "Contact imported", Toast.LENGTH_SHORT).show();
        }
        break;
    }
    case CConst.RESPONSE_CODE_SHARE_VCF: {

        LogUtil.log("SharedMenu sharedOnActivityResult SHARE_VCF delete ");
        Util.cleanupTempFolder(act);
        break;
    }
    default: {

        LogUtil.log("sharedOnActivityResult UNMANAGED/ERROR requestCode: " + requestCode);
    }
    }
}

From source file:org.alfresco.mobile.android.application.accounts.AccountManager.java

public static Account createAccount(Context context, String name, String url, String username, String pass,
        String workspace, Integer type, String activation, String accessToken, String refreshToken,
        int isPaidAccount) {
    Uri accountUri = context.getContentResolver().insert(AccountProvider.CONTENT_URI, createContentValues(name,
            url, username, pass, workspace, type, activation, accessToken, refreshToken, isPaidAccount));

    if (accountUri == null) {
        return null;
    }/*w ww . j a va 2s.c  o  m*/

    return AccountManager.retrieveAccount(context, Long.parseLong(accountUri.getLastPathSegment()));
}

From source file:org.creativecommons.thelist.utils.FileHelper.java

public static String getFileName(Context context, Uri uri, String fileType) {
    String fileName = "uploaded_file.";

    if (fileType.equals(PhotoConstants.MEDIA_TYPE_IMAGE)) {
        fileName += "png";
    } else {//from w  ww . j a  v  a  2  s  .  c  o m
        // For video, we want to get the actual file extension
        if (uri.getScheme().equals("content")) {
            // do it using the mime type
            String mimeType = context.getContentResolver().getType(uri);
            int slashIndex = mimeType.indexOf("/");
            String fileExtension = mimeType.substring(slashIndex + 1);
            fileName += fileExtension;
        } else {
            fileName = uri.getLastPathSegment();
        }
    }

    return fileName;
}

From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java

/**
 * Get the file name from a content Uri.
 *
 * @param cr  The ContentResolver/*from w  w  w .  ja  v a 2 s  .  c  om*/
 * @param uri The Uri
 * @return The file name
 */
@Nullable
private static String getName(@NonNull ContentResolver cr, @NonNull Uri uri) {
    if ("file".equals(uri.getScheme())) {
        return uri.getLastPathSegment();
    } else {
        final String[] projection = new String[] { MediaStore.MediaColumns.DISPLAY_NAME };
        final Cursor cursor = cr.query(uri, projection, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    return cursor.getString(0);
                }
            } finally {
                cursor.close();
            }
        }
    }
    return null;
}

From source file:org.matrix.matrixandroidsdk.db.ConsoleMediasCache.java

public static String mediaCacheFilename(Context context, String url, int width, int height) {
    // sanity check
    if (null == url) {
        return null;
    }/*from www  . ja v a2  s . co m*/

    String filename = BitmapWorkerTask.buildFileName(downloadableUrl(context, url, width, height));

    try {
        // already a local file
        if (filename.startsWith("file:")) {
            Uri uri = Uri.parse(filename);
            filename = uri.getLastPathSegment();
        }

        File file = new File(context.getApplicationContext().getFilesDir(), filename);

        if (!file.exists()) {
            filename = null;
        }

    } catch (Exception e) {
        filename = null;
    }

    return filename;
}

From source file:org.kde.kdeconnect.Plugins.SharePlugin.SharePlugin.java

private static NetworkPackage uriToNetworkPackage(final Context context, final Uri uri) {

    try {// w w w  . j a v  a  2 s. c o m

        ContentResolver cr = context.getContentResolver();
        InputStream inputStream = cr.openInputStream(uri);

        NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_SHARE_REQUEST);
        long size = -1;

        if (uri.getScheme().equals("file")) {
            // file:// is a non media uri, so we cannot query the ContentProvider

            np.set("filename", uri.getLastPathSegment());

            try {
                size = new File(uri.getPath()).length();
            } catch (Exception e) {
                Log.e("SendFileActivity", "Could not obtain file size");
                e.printStackTrace();
            }

        } else {
            // Probably a content:// uri, so we query the Media content provider

            Cursor cursor = null;
            try {
                String[] proj = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.SIZE,
                        MediaStore.MediaColumns.DISPLAY_NAME };
                cursor = cr.query(uri, proj, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                cursor.moveToFirst();
                String path = cursor.getString(column_index);
                np.set("filename", Uri.parse(path).getLastPathSegment());
                size = new File(path).length();
            } catch (Exception unused) {

                Log.w("SendFileActivity", "Could not resolve media to a file, trying to get info as media");

                try {
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
                    cursor.moveToFirst();
                    String name = cursor.getString(column_index);
                    np.set("filename", name);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("SendFileActivity", "Could not obtain file name");
                }

                try {
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
                    cursor.moveToFirst();
                    //For some reason this size can differ from the actual file size!
                    size = cursor.getInt(column_index);
                } catch (Exception e) {
                    Log.e("SendFileActivity", "Could not obtain file size");
                    e.printStackTrace();
                }
            } finally {
                try {
                    cursor.close();
                } catch (Exception e) {
                }
            }

        }

        np.setPayload(inputStream, size);

        return np;
    } catch (Exception e) {
        Log.e("SendFileActivity", "Exception sending files");
        e.printStackTrace();
        return null;
    }
}