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:app.view.chat.ChatActivity.java

/**
 * ??//from   w  w  w  . j  av a 2s  .  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.ccxt.whl.activity.SettingsFragment.java

/**
 *  ?uri??// w w  w . java  2s.  co m
 * 
 * @param selectedImage
 */
private File Uritofile(Uri selectedImage) {
    File file = null;
    Cursor cursor = getActivity().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(getActivity(), "?", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return null;
        }
        file = new File(picturePath);
        //sendPicture(picturePath);
    } else {
        file = new File(selectedImage.getPath());
        if (!file.exists()) {
            Toast toast = Toast.makeText(getActivity(), "?", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return null;
        }

    }

    return file;

}

From source file:com.roamprocess1.roaming4world.ui.messages.MessageActivity.java

public String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;/*from  w  w  w  .  j a v a  2  s  .co m*/

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ContentResolver cr;/* w w  w. j  a  va 2s  . c  o m*/
    InputStream is;
    if (requestCode == TAKE_PHOTO) {
        showProgress(false);
        if (resultCode == -1) {
            try {
                if (data != null) {
                    if (data.hasExtra("data")) {
                        //Bitmap bm = ;
                        drawView.setVisibility(View.INVISIBLE);
                        drawView.recycle();
                        drawView.setBitmap((Bitmap) data.getParcelableExtra("data"), 0);
                        drawView.setVisibility(View.VISIBLE);

                    }
                } else {
                    if (outputFileUri != null) {
                        cr = getContentResolver();
                        try {
                            is = cr.openInputStream(outputFileUri);
                            Bitmap bmp = BitmapFactory.decodeStream(is);
                            if (bmp.getWidth() != -1 && bmp.getHeight() != -1) {
                                drawView.setVisibility(View.INVISIBLE);
                                drawView.recycle();
                                drawView.setBitmap(bmp, 0);
                                drawView.setVisibility(View.VISIBLE);
                            }
                        } catch (Exception e) {
                            appSettings.appendLog("paint:onCreate  " + e.getMessage());
                        }
                    }
                }
            } catch (Exception e) {
                appSettings.appendLog("main:onActivityResult: exception -  " + e.getMessage());
            }
        }
        ((ViewAnimator) findViewById(R.id.top_switcher)).setDisplayedChild(0);
    } else if (requestCode == TAKE_PICTURE) {
        showProgress(false);
        if (resultCode == -1 && data != null) {
            boolean temp = false;
            try {
                Uri resultUri = data.getData();
                String drawString = resultUri.getPath();
                InputStream input = getContentResolver().openInputStream(resultUri);

                drawView.setVisibility(View.INVISIBLE);
                drawView.recycle();
                drawView.setBitmap(BitmapFactory.decodeStream(input), 0);
                //drawView.forceRedraw();
                drawView.setVisibility(View.VISIBLE);
            } catch (Exception e) {
                appSettings.appendLog("main:onActivityResult  " + e.getMessage());

            }
        }
        ((ViewAnimator) findViewById(R.id.top_switcher)).setDisplayedChild(0);
    } else if (requestCode == SHOW_SETTINGS) {
        switch (resultCode) {
        case RESULT_FIRST_USER + 1:
            Intent i = new Intent(getApplicationContext(), PrefsActivity.class);
            startActivity(i);
            //overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );
            overridePendingTransition(R.anim.carbon_slide_in, R.anim.carbon_slide_out);
            break;
        case RESULT_FIRST_USER + 2:
            if (appSettings.sessionId.length() == 0)
                showLogin();
            else {
                appSettings.sessionId = "";
                //serverHelper.getInstance().setSessionId(appSettings.sessionId);
                Editor e = prefs.edit();
                e.putString("userMail", userMail);
                e.putString("userPass", "");
                e.putString("sessionId", appSettings.sessionId);
                e.commit();
                showLogin();
            }
            break;
        case RESULT_FIRST_USER + 3:
            try {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("market://details?id=" + getApplicationInfo().packageName)));
            } catch (Exception e) {
            }
        case RESULT_FIRST_USER + 4:
            Uri uri = Uri.parse(
                    "http://help.everhelper.me/customer/portal/articles/1376820-nimbus-clipper-for-android---quick-guide");
            Intent it = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(it);
            //overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );
            overridePendingTransition(R.anim.carbon_slide_in, R.anim.carbon_slide_out);
            break;
        case RESULT_FIRST_USER + 5:
            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this,
                    AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
            alertDialogBuilder.setMessage(getScriptContent("license.txt")).setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // no alert dialog shown
                            //alertDialogShown = null;
                            // canceled
                            setResult(RESULT_CANCELED);
                            // and finish
                            //finish();
                        }
                    });
            // create alert dialog
            final AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.setTitle(getString(R.string.license_title));

            // and show
            //alertDialogShown = alertDialog;
            try {
                alertDialog.show();
            } catch (final java.lang.Exception e) {
                // nothing to do
            } catch (final java.lang.Error e) {
                // nothing to do
            }
            break;
        default:
            break;
        }
    }

    if (requestCode == 3) {
        if (resultCode == RESULT_OK || resultCode == RESULT_FIRST_USER) {
            userMail = data.getStringExtra("userMail");
            userPass = data.getStringExtra("userPass");
            if (resultCode == RESULT_OK)
                sendRequest("user:auth",
                        String.format("\"email\":\"%s\",\"password\":\"%s\"", userMail, userPass));
            else
                serverHelper.getInstance().sendOldRequest("user_register", String.format(
                        "{\"action\": \"user_register\",\"email\":\"%s\",\"password\":\"%s\",\"_client_software\": \"ff_addon\"}",
                        userMail, userPass), "");
        }
    } else if (requestCode == 11) {
        if (appSettings.sessionId != "") {
            sessionId = appSettings.sessionId;
            userPass = appSettings.userPass;
            sendShot();
        }
    } else if (requestCode == 4) {
        if (resultCode == RESULT_OK) {
            String id = data.getStringExtra("id").toString();
            serverHelper.getInstance().shareShot(id);
        }
    } else if (resultCode == RESULT_OK) {
        drawView.setColour(dColor);
        setPaletteColor(dColor);
        Uri resultUri = data.getData();
        String drawString = resultUri.getPath();
        String galleryString = getGalleryPath(resultUri);

        if (galleryString != null) {
            drawString = galleryString;
        }
        // else another file manager was used
        else {
            if (drawString.contains("//")) {
                drawString = drawString.substring(drawString.lastIndexOf("//"));
            }
        }

        // set the background to the selected picture
        if (drawString.length() > 0) {
            Drawable drawBackground = Drawable.createFromPath(drawString);
            drawView.setBackgroundDrawable(drawBackground);
        }

    }
}

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

/**
 * ?uri?//  w  ww  . jav a  2 s.c  o  m
 * 
 * @param selectedImage
 */
private String getpathfromUri(Uri selectedImage) {
    // String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().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(getActivity(), "?", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return null;
        }
        return picturePath;
        //sendPicture(picturePath);
    } else {
        File file = new File(selectedImage.getPath());
        if (!file.exists()) {
            Toast toast = Toast.makeText(getActivity(), "?", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return null;

        }
        return file.getAbsolutePath();
    }

}

From source file:ac.robinson.mediaphone.MediaPhoneActivity.java

private void exportMovie(final Map<Integer, Object> settings, final String exportName,
        final ArrayList<FrameMediaContainer> contentList) {
    runExportNarrativesTask(new BackgroundRunnable() {
        // mov export is a special case - the id matters at task start time (so we can show the right dialog)
        private int mTaskResult = R.id.export_mov_task_complete;

        @Override//  www.ja va2s  .c o  m
        public int getTaskId() {
            return mTaskResult;
        }

        @Override
        public boolean getShowDialog() {
            return true;
        }

        @Override
        public void run() {
            ArrayList<Uri> movFiles = MOVUtilities.generateNarrativeMOV(getResources(),
                    new File(MediaPhone.DIRECTORY_TEMP, exportName + MediaUtilities.MOV_FILE_EXTENSION),
                    contentList, settings);

            // must use media store parameters properly, or YouTube export fails
            // see: http://stackoverflow.com/questions/5884092/
            ArrayList<Uri> filesToSend = new ArrayList<Uri>();
            for (Uri fileUri : movFiles) {
                File outputFile = new File(fileUri.getPath());
                ContentValues content = new ContentValues(5);
                content.put(MediaStore.Video.Media.DATA, outputFile.getAbsolutePath());
                content.put(MediaStore.Video.VideoColumns.SIZE, outputFile.length());
                content.put(Video.VideoColumns.DATE_ADDED, System.currentTimeMillis() / 1000);
                content.put(Video.Media.MIME_TYPE, "video/quicktime");
                content.put(Video.VideoColumns.TITLE, IOUtilities.removeExtension(outputFile.getName()));
                filesToSend
                        .add(getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content));
            }

            if (filesToSend == null || filesToSend.size() <= 0) {
                mTaskResult = R.id.export_creation_failed;
            } else {
                sendFiles(filesToSend);
            }
        }
    });
}

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

/**
 * ??/*from ww  w  . j  a  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()) {
        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.liangxun.yuejiula.huanxin.chat.activity.ChatOldActivity.java

/**
 * ??/*  w  w  w .  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(), "?", 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

/**
 * //from   w ww  .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()) {
        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.futurologeek.smartcrossing.crop.CropImageActivity.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public 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];
            }//from w ww. j a  v a2  s  . co m
        }
        // 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;
}