Example usage for android.net Uri getScheme

List of usage examples for android.net Uri getScheme

Introduction

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

Prototype

@Nullable
public abstract String getScheme();

Source Link

Document

Gets the scheme of this URI.

Usage

From source file:com.liangxun.yuejiula.huanxin.chat.activity.ChatOldActivity.java

/**
 * ??/*from  ww  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()) {
        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);
    // 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.interestfriend.activity.FeedBackActivity.java

/**
 * //  w  ww .java2  s . 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(), "", 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(message);
    listView.setAdapter(adapter);
    adapter.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
}

From source file:com.luorrak.ouroboros.reply.ReplyCommentFragment.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders./*from w  ww . j ava2  s.c om*/
 *
 * @param context The context.
 * @param uri The Uri to query.
 * @author paulburke
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getPath(final Context context, final 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];
            }

            // 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 getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:com.xxjwd.chat.ChatActivity.java

 /**
 * ??/*from  w  w  w. ja v a  2 s.  com*/
 * 
 * @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;
   }

   // ?
   ChatMessage message = ChatMessage.createSendMessage(ChatMessage.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:Main.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders./*from www . jav  a 2 s. co m*/
 *
 * @param context The context.
 * @param uri The Uri to query.
 */
public static String getPath(final Context context, final Uri uri) {

    // DocumentProvider
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT // is Kitkat or later
            && 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];
            }

        }
        // 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);
        }
        // DriveDocument
        else if (isDriveDocument(uri)) {
            // I have not found a way to generate the absolute url.
            // Check from outside if it's a GoogleDrive document.
            // Generate a bitmap and convert to bytes.
            return null;
        }
    }
    // 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.blueshit.activity.ChatActivity.java

/**
 * ??//from  ww w.  jav  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);

    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.mediatek.systemupdate.HttpManager.java

private HttpResponse doPost(String url, Map<String, String> headers, ArrayList<BasicNameValuePair> bnvpa) {

    Xlog.i(TAG, "doPost, url = " + url + ", mCookies = " + mCookies);
    HttpContext localcontext = new BasicHttpContext();
    if (mCookies != null) {
        localcontext.setAttribute(ClientContext.COOKIE_STORE, mCookies);
    }/*from  ww  w  . j  av  a 2s .c o m*/
    HttpResponse response = null;
    try {
        HttpHost host = null;
        HttpPost httpPost = null;

        if (url.contains("https")) {
            Uri uri = Uri.parse(url);
            host = new HttpHost(uri.getHost(), PORT_NUMBER, uri.getScheme());
            httpPost = new HttpPost(uri.getPath());
        } else {
            httpPost = new HttpPost(url);
        }

        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpPost.addHeader(entry.getKey(), entry.getValue());
            }
        }

        if (bnvpa != null) {
            httpPost.setEntity(new UrlEncodedFormEntity(bnvpa));
        }
        DefaultHttpClient httpClient = new DefaultHttpClient(mHttpConnMgr, mHttpParam);

        try {
            if (url.contains("https")) {
                Xlog.i(TAG, "doPost, https");
                response = httpClient.execute(host, httpPost);
            } else {
                Xlog.i(TAG, "doPost, http");
                Xlog.i(TAG, "mHttpClient =" + httpClient + "httpPost = " + httpPost + "localcontext = "
                        + localcontext);
                response = httpClient.execute(httpPost, localcontext);
            }
            if (mCookies == null) {
                mCookies = httpClient.getCookieStore();
                Xlog.i(TAG, "mCookies size = " + mCookies.getCookies().size());
            }
            return response;
        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
            mErrorCode = HTTP_RESPONSE_NETWORK_ERROR;
        }

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();
        mErrorCode = HTTP_RESPONSE_NETWORK_ERROR;
    } catch (IOException e) {
        e.printStackTrace();
        mErrorCode = HTTP_RESPONSE_NETWORK_ERROR;
    }
    return response;
}

From source file:com.wenwen.chatuidemo.activity.ChatActivity.java

/**
 * ??//  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()) {
        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("accountname", DemoApplication.getInstance().getAcount_name());
    message.setAttribute("toaccountname", toAccountname);
    message.setAttribute("type", "1");
    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.xxxifan.devbox.library.rxfile.RxFile.java

private static Observable<Bitmap> getThumbnailFromUriWithSizeAndKind(final Context context, final Uri data,
        final int requiredWidth, final int requiredHeight, final int kind) {
    return Observable.fromCallable(new Func0<Bitmap>() {
        @Override/*from  w w  w .j a  va2s  .c o m*/
        public Bitmap call() {
            Bitmap bitmap = null;
            ParcelFileDescriptor parcelFileDescriptor;
            final BitmapFactory.Options options = new BitmapFactory.Options();
            if (requiredWidth > 0 && requiredHeight > 0) {
                options.inJustDecodeBounds = true;
                options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
                options.inJustDecodeBounds = false;
            }
            if (!isMediaUri(data)) {
                Log.e(TAG, "Not a media uri");
                if (isGoogleDriveDocument(data)) {
                    Log.e(TAG, "Google Drive Uri");
                    DocumentFile file = DocumentFile.fromSingleUri(context, data);
                    if (file.getType().startsWith(Constants.IMAGE_TYPE)
                            || file.getType().startsWith(Constants.VIDEO_TYPE)) {
                        Log.e(TAG, "Google Drive Uri");
                        try {
                            parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data,
                                    Constants.READ_MODE);
                            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                            bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
                            parcelFileDescriptor.close();
                            return bitmap;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } else if (data.getScheme().equals(Constants.FILE)) {
                    Log.e(TAG, "Dropbox or other content provider");
                    try {
                        parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data,
                                Constants.READ_MODE);
                        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                        bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
                        parcelFileDescriptor.close();
                        return bitmap;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data,
                                Constants.READ_MODE);
                        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                        bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
                        parcelFileDescriptor.close();
                        return bitmap;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                Log.e(TAG, "Uri for thumbnail: " + data.toString());
                Log.e(TAG, "Uri for thumbnail: " + data);
                String[] parts = data.getLastPathSegment().split(":");
                String fileId = parts[1];
                Cursor cursor = null;
                try {
                    cursor = context.getContentResolver().query(data, null, null, null, null);
                    if (cursor != null) {
                        Log.e(TAG, "Cursor size: " + cursor.getCount());
                        if (cursor.moveToFirst()) {
                            if (data.toString().contains(Constants.VIDEO)) {
                                bitmap = MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),
                                        Long.parseLong(fileId), kind, options);
                            } else if (data.toString().contains(Constants.IMAGE)) {
                                Log.e(TAG, "Image Uri");
                                bitmap = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
                                        Long.parseLong(fileId), kind, options);
                            }
                            Log.e(TAG, bitmap == null ? "null" : "not null");
                        }
                    }
                    return bitmap;
                } catch (Exception e) {
                    Log.e(TAG, "Exception while getting thumbnail:" + e.getMessage());
                } finally {
                    if (cursor != null)
                        cursor.close();
                }
            }
            return bitmap;
        }
    });
}

From source file:com.siso.app.chat.ui.ChatActivity.java

/**
 * ??/*w ww. jav  a  2  s  .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()) {
        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.refreshSelectLast();
    setResult(RESULT_OK);
}