简体   繁体   English

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

[英]Get image name from selected image in gallery

At the moment I am selecting an image from gallery and the whole path is shown example (/storage/emulated.0/DCIM/Camera/123.jpg) in the textview whereas I only want the name of the image to be shown example 123.jpg. 目前,我正在从图库中选择图像,整个路径在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");
}

Followed this link Get filepath and filename of selected gallery image in Android 通过此链接获取Android中所选图库图像的文件路径和文件名

You need to perform a String operation.follwoing code will help you out. 您需要执行String操作。下面的代码将为您提供帮助。

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

possible duplicate of : How to get file name from file path in android 可能的重复项: 如何从android中的文件路径获取文件名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM