从库中的选定图像中获取图像名称

时间:2016-03-30 12:20:47

标签: android

目前我正在从图库中选择一个图像,整个路径在textview中显示示例(/storage/emulated.0/DCIM/Camera/123.jpg),而我只想要图像的名称示例123.jpg。

public String getRealPathFromURI(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    BitmapFactory.Options options;
    if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) {
        try {
            options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Uri selectedImage = data.getData();
            String s= getRealPathFromURI(selectedImage);
            imageName.append(s);
            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
            String[] filePath = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String mImagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            InputStream stream = getContentResolver().openInputStream(selectedImage);
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options);
            stream.close();
            //---orientation---
            try {
                int rotate = 0;
                try {
                    ExifInterface exif = new ExifInterface(
                            mImagePath);
                    dateTaken.append(exif.getAttribute(ExifInterface.TAG_DATETIME));
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);

                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            rotate = 270;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            rotate = 180;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            rotate = 90;
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Matrix matrix = new Matrix();
                matrix.postRotate(rotate);
                yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true);

            }
            catch (Exception e) {}
            //---end of orientation---

            imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imgButton.setImageBitmap(yourSelectedImage);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show();
    }
    Log.i(TAG, "onActivityResult - Portal");
}

点击此链接Get filepath and filename of selected gallery image in Android

1 个答案:

答案 0 :(得分:2)

你需要执行一个字符串操作。字体代码将帮助你。

    String path=":/storage/sdcard0/DCIM/Camera/1414240995236.jpg";
   String filename=path.substring(path.lastIndexOf("/")+1);

可能重复: How to get file name from file path in android