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.kdao.cmpe235_project.UploadActivity.java

@SuppressLint("NewApi")
private String getPath(Uri uri) throws URISyntaxException {
    final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
    String selection = null;/*  w  w w  .j  ava  2s.co  m*/
    String[] selectionArgs = null;
    // Uri is different in versions after KITKAT (Android 4.4), we need to
    // deal with different Uris.
    if (needToCheckUri && DocumentsContract.isDocumentUri(getApplicationContext(), uri)) {
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            return Environment.getExternalStorageDirectory() + "/" + split[1];
        } else if (isDownloadsDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            uri = ContentUris.withAppendedId(Uri.parse(LOCAL_FOLDER_PATH), Long.valueOf(id));
        } else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            if ("image".equals(type)) {
                uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }
            selection = "_id=?";
            selectionArgs = new String[] { split[1] };
        }
    }
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.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:com.lgallardo.youtorrentcontroller.RefreshListener.java

public static String getFilePathFromUri(Context c, Uri uri) {
    String filePath = null;//from w w  w  .  j a v a  2 s  .  c  o m
    if ("content".equals(uri.getScheme())) {
        String[] filePathColumn = { MediaStore.MediaColumns.DATA };
        ContentResolver contentResolver = c.getContentResolver();

        Cursor cursor = contentResolver.query(uri, filePathColumn, null, null, null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();
    } else if ("file".equals(uri.getScheme())) {
        filePath = new File(uri.getPath()).getAbsolutePath();
    }
    return filePath;
}

From source file:com.grass.caishi.cc.activity.ChatActivity.java

/**
 * ?uri??//from   w w w  . j  a v a  2s  .c  om
 * 
 * @param selectedImage
 */
private void sendPicByUri(Uri selectedImage) {

    //
    //
    // String[] filePathColumn = { MediaStore.Images.Media.DATA };
    // Cursor cursor = getContentResolver().query(filePath,filePathColumn,
    // null, null, null);
    // cursor.moveToFirst();
    // int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    // String picturePath = cursor.getString(columnIndex);
    // cursor.close();
    // return picturePath;
    //
    //

    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    if (cursor != null) {

        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        cursor = null;

        if (picturePath == null || picturePath.equals("null")) {
            Toast toast = Toast.makeText(this, "?", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return;
        }
        sendPicture(picturePath);
    } else {
        File file = new File(selectedImage.getPath());
        if (!file.exists()) {
            Toast toast = Toast.makeText(this, "?", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return;

        }
        sendPicture(file.getAbsolutePath());
    }
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

@SuppressLint("NewApi")
public static String getPath(final Uri uri) {
    try {/*from www  . j av  a  2 s.  c  o m*/
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        if (isKitKat && DocumentsContract.isDocumentUri(ApplicationLoader.applicationContext, uri)) {
            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];
                }
            } 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(ApplicationLoader.applicationContext, contentUri, null, null);
            } else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                switch (type) {
                case "image":
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    break;
                case "video":
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    break;
                case "audio":
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    break;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[] { split[1] };

                return getDataColumn(ApplicationLoader.applicationContext, contentUri, selection,
                        selectionArgs);
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(ApplicationLoader.applicationContext, uri, null, null);
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    return null;
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static String getImagePathFromUri(final Context context, final Uri uri) {
    if (context == null || uri == null)
        return null;

    final String media_uri_start = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString();

    if (uri.toString().startsWith(media_uri_start)) {

        final String[] proj = { MediaStore.Images.Media.DATA };
        final Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);

        if (cursor == null || cursor.getCount() <= 0)
            return null;

        final int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();/*www .j  a  v a2 s .  c o  m*/

        final String path = cursor.getString(column_index);
        cursor.close();
        return path;
    } else {
        final String path = uri.getPath();
        if (path != null) {
            if (new File(path).exists())
                return path;
        }
    }
    return null;
}

From source file:com.wiret.arbrowser.AbstractArchitectCamActivity.java

@SuppressLint("NewApi")
private String getPath(Context context, Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && 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];
            }//from   w  ww . j ava  2  s.co m
            // 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;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.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 (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();
        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

/**
 * This is called after the file manager finished.
 *//*from  w w  w  . j  a v  a  2 s . c om*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK && data != null) {
        // obtain the filename
        Uri fileUri = data.getData();
        if (fileUri != null) {
            String filePath = fileUri.getPath();
            if (filePath != null) {
                loadNewBook(filePath);
            }
        }
    }
}

From source file:com.haomee.chat.activity.ChatActivity.java

/**
 * ??//from ww w. ja  v a2  s  .  c o  m
 * 
 * @param uri
 */
private void sendFile(Uri uri) {
    String filePath = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        filePath = uri.getPath();
    }
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        MyToast.makeText(getApplicationContext(), "?", 0).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        MyToast.makeText(getApplicationContext(), "?10M", 0).show();
        return;
    }

    // ?
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // ?chattype,??
    if (chatType == CHATTYPE_GROUP)
        message.setChatType(ChatType.GroupChat);

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
    message.addBody(body);

    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
}

From source file:com.easemob.ui.ChatActivity.java

/**
 * ??/*from w w  w . j a va2s .com*/
 * @param videoUri
 */
private void sendVideoMsg(Uri videoUri) {
    String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Video.Media.DURATION };
    try {
        Cursor cursor = getContentResolver().query(videoUri, proj, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                String videoPath = cursor.getString(index);
                Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, 3);
                if (bitmap == null) {
                    EMLog.d("chatactivity", "problem load video thumbnail bitmap,use default icon");
                    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.app_panel_video_icon);
                }
                File videoFile = new File(videoPath);
                System.out.println("length:" + videoFile.length());
                // ???10M
                if (videoFile.length() > 1024 * 1024 * 10) {
                    Toast.makeText(this, "??10M?", Toast.LENGTH_SHORT).show();
                    return;
                }

                // get the thumb image file
                File file = new File(PathUtil.getInstance().getVideoPath(), "th" + videoFile.getName());
                try {
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                sendVideo(videoPath, file.getAbsolutePath(), duration);
            }
        } else {
            File videoFile = new File(videoUri.getPath());
            Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoFile.getAbsolutePath(), 3);
            if (bitmap == null) {
                EMLog.d("chatactivity", "problem load video thumbnail bitmap,use default icon");
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.app_panel_video_icon);
            }

            System.out.println("length:" + videoFile.length());
            // ???10M
            if (videoFile.length() > 1024 * 1024 * 10) {
                Toast.makeText(this, "??10M?", Toast.LENGTH_SHORT).show();
                return;
            }

            // get the thumb image file
            File file = new File(PathUtil.getInstance().getVideoPath(), "th" + videoFile.getName());
            try {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
            } catch (Exception e) {
                e.printStackTrace();
            }
            sendVideo(videoFile.getAbsolutePath(), file.getAbsolutePath(), 0);

        }

    } catch (Exception e) {
        System.out.println("exception:" + e.getMessage());
    }
}