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.givon.anhao.activity.ChatActivity.java

/**
 * ??/*from ww  w  .ja  v a  2 s  . c  om*/
 * 
 * @param uri
 */
private void sendFile(Uri uri) {
    addFriend(mUserBean, chatType);
    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()) {
        Toast.makeText(getApplicationContext(), "?", 0).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        Toast.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(initExtValue(message));
    listView.setAdapter(adapter);
    adapter.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
}

From source file:com.aimfire.gallery.GalleryActivity.java

/**
 * handle opening of a cvr file already on the phone. if it is not in our directory
 * structure, we copy it there first./*  w w  w .j  av a 2s  . c  o m*/
 * 
 * @param uri
 * @return path to the cvr file in our directory structure
 */
private String handleLocalOpen(Uri uri) {
    String path = null;

    /*
    * we are opened from file broswer
    */
    File f = new File(uri.getPath());
    if (f != null) {
        path = f.getAbsolutePath();
    }

    /*
     * we support file open of cvr file only
     */
    if ((path == null) || !MediaScanner.isMovie(path)) {
        Toast.makeText(this, R.string.error_incorrect_link, Toast.LENGTH_LONG).show();
        return null;
    }

    if (path.contains(MainConsts.MEDIA_3D_SAVE_PATH)) {
        mIsMyMedia = true;
    } else if (path.contains(MainConsts.MEDIA_3D_SHARED_PATH)) {
        mIsMyMedia = false;
    } else {
        mIsMyMedia = false;

        /*
         * in case we got here directly without going thru MainActivity
         * first, it's possible we don't have storage initialized yet.
         */
        if (FileUtils.initStorage()) {
            /*
             * we are launched from external apps by file type. move the file 
               * to our root dir. if rename fails, we will have to do the actual 
               * copy (rather than rename, as we may be copying the file from
               * internal to external storage; or we don't have write permission
               * on the source directory)
             */
            String newFilePath = MainConsts.MEDIA_3D_SHARED_PATH + (new File(path)).getName();
            boolean success = false;
            try {
                File from = (new File(path));
                File to = (new File(newFilePath));
                success = from.renameTo(to);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (!success) {
                FileUtils.copyFile(path, newFilePath);
            }

            path = newFilePath;

            /*
             * make MediaScanner aware of the new file
             */
            MediaScanner.addItemMediaList(path);
        } else {
            Toast.makeText(this, R.string.error_accessing_storage, Toast.LENGTH_LONG).show();
            return null;
        }
    }

    /*
     * import/convert the file if it has the old format (.png preview frame)
     */
    if (MediaScanner.is3dMovie(path)) {
        convertCvr(path);
    }

    return path;
}

From source file:com.interestfriend.activity.ChatActivity.java

/**
 * //  www.j  a  v  a2  s. co 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()) {
        Toast.makeText(getApplicationContext(), "", 0).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        Toast.makeText(getApplicationContext(), "10M", 0).show();
        return;
    }

    // 
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // chattype,
    if (chatType == CHATTYPE_GROUP)
        message.setChatType(ChatType.GroupChat);
    message.setAttribute("user_name", SharedUtils.getAPPUserName());
    message.setAttribute("user_avatar", SharedUtils.getAPPUserAvatar());
    message.setAttribute("circle_name", MyApplation.getCircle_name());
    message.setAttribute("user_id", SharedUtils.getIntUid());

    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.ieeton.user.activity.ChatActivity.java

/**
 * ??// ww w  .ja v  a 2s.c  om
 * 
 * @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()) {
        Toast.makeText(getApplicationContext(), "?", Toast.LENGTH_SHORT).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        Toast.makeText(getApplicationContext(), "?10M", Toast.LENGTH_SHORT).show();
        return;
    }

    // ?
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);

    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.erhuoapp.erhuo.activity.ChatActivity.java

/**
 * ??//from  w  w w .ja  va  2  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()) {
        String st7 = getResources().getString(R.string.File_does_not_exist);
        Toast.makeText(getApplicationContext(), st7, 0).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
        Toast.makeText(getApplicationContext(), st6, 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);

    EntityUserInfo userInfo = AppUtil.getInstance().getBasicUserInfo();
    message.setAttribute("from", userInfo.getNickName());
    message.setAttribute("fromheader", userInfo.getHeader());
    message.setAttribute("fromid", userInfo.getId());

    String id = getIntent().getStringExtra("userId");
    HeadAndName mHeadAndName = new HeadAndName(this);
    UserHeadAndName mUserHeadAndName = mHeadAndName.selectOne(id);
    message.setAttribute("to", mUserHeadAndName.getNick());
    message.setAttribute("toheader", mUserHeadAndName.getHead());

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

From source file:com.fvd.nimbus.PaintActivity.java

private String getImagePath() {

    String[] projection = { MediaStore.Images.Thumbnails._ID, // The columns we want
            MediaStore.Images.Thumbnails.IMAGE_ID, MediaStore.Images.Thumbnails.KIND,
            MediaStore.Images.Thumbnails.DATA };
    String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select only mini's
            MediaStore.Images.Thumbnails.MINI_KIND;

    String sort = MediaStore.Images.Thumbnails._ID + " DESC";

    //At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable
    Cursor myCursor = this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection,
            selection, null, sort);// w  w  w  .  j  ava2s . c om

    long imageId = 0l;
    long thumbnailImageId = 0l;
    String thumbnailPath = "";

    try {
        myCursor.moveToFirst();
        imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
        thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
        thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
    } finally {
        myCursor.close();
    }

    //Create new Cursor to obtain the file Path for the large image

    String[] largeFileProjection = { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA };

    String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
    myCursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null,
            largeFileSort);
    String largeImagePath = "";

    try {
        myCursor.moveToFirst();

        largeImagePath = myCursor
                .getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
    } finally {
        myCursor.close();
    }
    // These are the two URI's you'll be interested in. They give you a handle to the actual images
    Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            String.valueOf(imageId));
    Uri uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            String.valueOf(thumbnailImageId));

    if (largeImagePath.length() > 0)
        return largeImagePath;
    else if (uriLargeImage != null)
        return uriLargeImage.getPath();
    else if (uriThumbnailImage != null)
        return uriThumbnailImage.getPath();
    else
        return "";

}

From source file:com.ccxt.whl.activity.ChatActivity.java

/**
 * ?uri??
 * /* w  w  w .j  a va 2 s . co m*/
 * @param selectedImage
 */
private void sendPicByUri(Uri selectedImage) {
    // String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(selectedImage, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex("_data");
        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:cn.kangeqiu.kq.activity.ChatActivity.java

/**
 * ??/*from  ww  w. j  a  va2s  .  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()) {
        String st7 = getResources().getString(R.string.File_does_not_exist);
        Toast.makeText(getApplicationContext(), st7, 0).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
        Toast.makeText(getApplicationContext(), st6, 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.example.Bama.chat.chatuidemo.activity.ChatActivity.java

/**
 * ??//from  w w  w. j a  v  a  2 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()) {
        String st7 = getResources().getString(R.string.File_does_not_exist);
        Toast.makeText(getApplicationContext(), st7, Toast.LENGTH_SHORT).show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
        Toast.makeText(getApplicationContext(), st6, Toast.LENGTH_SHORT).show();
        return;
    }

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

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
    message.addBody(body);
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refreshSelectLast();
    setResult(RESULT_OK);
}

From source file:com.tct.mail.providers.Attachment.java

public static String getPath(final Context context, final Uri uri) {
    // DocumentProvider
    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];
            }//from   w  w  w  . ja v  a  2  s. c  o 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;
}