【问题标题】:Getting image extension获取图像扩展名
【发布时间】:2012-09-21 14:54:43
【问题描述】:

在应用程序中,用户可以选择图像。要启动画廊,我使用以下代码:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);

当我在 onActivityResult 方法中打印路径时,我得到像 /external/images/media/5615 这样的图像路径。我想知道图片扩展名,因为图片需要上传到服务器。扩展名可能会有所不同。

【问题讨论】:

    标签: java android image gallery uri


    【解决方案1】:

    您得到的是 MediaStore 用于获取图像的路径。以下代码将从您的 Intent 中获取结果,并获取附加到它的文件名:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == ACTIVITY_SELECT_IMAGE && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                Log.i(TAG, "" + selectedImage.toString());
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
    
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
    
                Log.i(TAG, "" + picturePath);
    
                //Do whatever with picturePath
    
            }
    

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      相关资源
      最近更新 更多