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

Posted

技术标签:

【中文标题】从图库中的选定图像中获取图像名称【英文标题】:Get image name from selected image in gallery 【发布时间】:2016-07-18 10:47:45 【问题描述】:

目前我正在从图库中选择图像,整个路径在 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】:

你需要执行一个字符串操作。下面的代码会帮助你。

    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

【讨论】:

以上是关于从图库中的选定图像中获取图像名称的主要内容,如果未能解决你的问题,请参考以下文章

从图库中获取图像以在片段中的图像视图中设置? [复制]

使用片段从图库中选择图像

获取Android中所选图库图像的文件路径和文件名

Qt 和 Android - 从图库中的图像获取路径

Android:从图库中获取图像,在回收器适配器中显示它们

如何从图库中的图像中获取(提取)文本并搜索该文本 - Android?