Example usage for android.provider DocumentsContract getDocumentId

List of usage examples for android.provider DocumentsContract getDocumentId

Introduction

In this page you can find the example usage for android.provider DocumentsContract getDocumentId.

Prototype

public static String getDocumentId(Uri documentUri) 

Source Link

Document

Extract the Document#COLUMN_DOCUMENT_ID from the given URI.

Usage

From source file:com.tct.mail.compose.ComposeActivity.java

@SuppressLint("NewApi")
public String getFilePath(Uri uri) {
    Uri thisUri = null;/* w w w  .  j  a  v  a  2  s.c  o  m*/
    String path = "";

    ContentResolver cr = this.getContentResolver();
    if (uri == null) {
        return path;
    }
    thisUri = uri;

    try {

        String scheme = thisUri.getScheme();

        if (scheme == null) {
            path = thisUri.toString();
        } else if (scheme.equals("file")) {
            path = thisUri.getPath();
            path = changeDrmFileSuffix(path);
        }

        else if (DocumentsContract.isDocumentUri(this, 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];
                }
            }
            // 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(this, 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;
                }
                contentUri = contentUri.withAppendedPath(contentUri, split[1]);
                return getDataColumn(this, contentUri, null, null);
            }
        }

        else if (scheme.equals("content")) {
            String[] projection = { "_data" };
            Cursor c = cr.query(thisUri, projection, null, null, null);
            if (c != null) {
                try {
                    if (c.moveToFirst()) {
                        path = c.getString(0);
                    }
                } finally {
                    c.close();
                }
            }

            if (path.endsWith("RAW")) {
                List<String> segments = thisUri.getPathSegments();
                String dbName = segments.get(0);
                String id = segments.get(1);
                path = this.getDatabasePath(dbName + "_att") + "/" + id;
            }

        }
    } catch (Exception e) {
    }
    return path;
}