Android 4.4(KitKat)上的Android Gallery为Intent.ACTION_GET_CONTENT返回不同的URI

Mic*_*der 212 android android-intent android-contentresolver android-gallery

在KitKat之前(或在新Gallery之前)Intent.ACTION_GET_CONTENT返回这样的URI

内容://媒体/外部/图像/媒体/ 3951.

使用ContentResolver和quering MediaStore.Images.Media.DATA返回文件URL.

但是在KitKat中,Gallery会返回一个URI(通过"Last"),如下所示:

内容://com.android.providers.media.documents/document/image:3951

我该如何处理?

Pau*_*rke 174

这不需要特殊权限,并且可以使用Storage Access Framework以及非官方ContentProvider模式(_data字段中的文件路径).

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.
 *
 * @param context The context.
 * @param uri The Uri to query.
 * @author paulburke
 */
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];
            }

            // TODO handle non-primary volumes
        }
        // 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;
}

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context The context.
 * @param uri The Uri to query.
 * @param selection (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
public static String getDataColumn(Context context, Uri uri, String selection,
        String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = {
            column
    };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}


/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is ExternalStorageProvider.
 */
public static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is DownloadsProvider.
 */
public static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is MediaProvider.
 */
public static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is Google Photos.
 */
public static boolean isGooglePhotosUri(Uri uri) {
    return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
Run Code Online (Sandbox Code Playgroud)

在此处查看此方法的最新版本.

  • 这太可怕了!你不应该继续传播像这样"欺骗"的代码.它只支持您知道模式的源应用程序,文档提供程序模型的重点是支持任意源 (29认同)
  • 这非常适用于4.4 Nexus 5 Documents UI以及使用标准图库应用程序的其他pref KitKat设备,感谢Paul! (2认同)
  • @RuAware当你选择一个Drive文件时,它会返回`Authority:com.google.android.apps.docs.storage`和`Segments:[document,acc = 1; doc = 667]`.我不确定,但假设`doc`值是你可以查询的`Uri` ID.您可能需要设置权限,详见"在Android上授权您的应用"中的详细信息:https://developers.google.com/drive/integrate-android-ui.如果你弄明白,请在这里更新. (2认同)
  • 当ContentProvider不支持时,`_data`将不起作用.建议遵循[@CommonsWare说明](https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html)并且不再使用完整文件路径,因为它可能是文件在Dropbox云中而不是真实文件. (2认同)

小智 107

试试这个:

if (Build.VERSION.SDK_INT <19){
    Intent intent = new Intent(); 
    intent.setType("image/jpeg");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);
} else {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/jpeg");
    startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;
    if (null == data) return;
    Uri originalUri = null;
    if (requestCode == GALLERY_INTENT_CALLED) {
        originalUri = data.getData();
    } else if (requestCode == GALLERY_KITKAT_INTENT_CALLED) {
        originalUri = data.getData();
        final int takeFlags = data.getFlags()
                & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        // Check for the freshest data.
        getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
    }

    loadSomeStreamAsynkTask(originalUri);

}
Run Code Online (Sandbox Code Playgroud)

可能需要

@SuppressLint( "NewApi")

对于

takePersistableUriPermission

  • 似乎我们无法从新的SDA uri获得路径.同样令人遗憾的是谷歌在没有适当的文件和声明的情况下做出了这种改变. (67认同)
  • 这不适合我.我得到以下异常(在股票4.4.2):E/AndroidRuntime(29204):引起:java.lang.SecurityException:请求标志0x1,但只允许0x0 (8认同)
  • 基于KitKat文档(http://developer.android.com/about/versions/android-4.4.html#UserContent),这可能不是OP所需要的,除非他打算使用/编辑其他人拥有的文档. (多个)应用.如果OP需要副本或以与旧版本一致的方式执行操作,@ voytez的答案将更合适. (4认同)
  • 这对我也没有用,originalUri = data.getData()总是为null .. (2认同)

voy*_*tez 67

有同样的问题,尝试了上面的解决方案,但虽然它一般工作,由于某种原因我得到权限拒绝Uri内容提供商的一些图像虽然我已android.permission.MANAGE_DOCUMENTS正确添加权限.

无论如何找到其他解决方案,强制打开图像库而不是KITKAT文档视图:

// KITKAT

i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, CHOOSE_IMAGE_REQUEST);
Run Code Online (Sandbox Code Playgroud)

然后加载图像:

Uri selectedImageURI = data.getData();
input = c.getContentResolver().openInputStream(selectedImageURI);
                BitmapFactory.decodeStream(input , null, opts);
Run Code Online (Sandbox Code Playgroud)

编辑

ACTION_OPEN_DOCUMENT 可能要求您持久保存权限标志等,并且通常会导致安全例外...

其他解决方案是使用ACTION_GET_CONTENT组合使用c.getContentResolver().openInputStream(selectedImageURI)它将在pre-KK和KK上工作.Kitkat将使用新的文档视图,此解决方案将适用于所有应用程序,如照片,图库,文件资源管理器,Dropbox,谷歌驱动器等...)但请记住,使用此解决方案时,您必须在您的图像中创建图像onActivityResult()并将其存储在例如SD卡.即使您添加了Google API文档中描述的权限标记(在我进行某些测试时发生的情况),在下次应用启动时从已保存的uri重新创建此图像也会在内容解析器上引发安全例外

此外,Android Developer API指南建议:

ACTION_OPEN_DOCUMENT不能替代ACTION_GET_CONTENT.您应该使用的那个取决于您的应用程序的需求:

如果您希望应用程序只读取/导入数据,请使用ACTION_GET_CONTENT.使用此方法,应用程序将导入数据的副本,例如图像文件.

如果您希望应用程序对文档提供程序拥有的文档具有长期,持久的访问权限,请使用ACTION_OPEN_DOCUMENT.一个例子是照片编辑应用程序,它允许用户编辑存储在文档提供程序中的图像.

  • 也为我工作,而不是'Intent.ACTION_GET_CONTENT`.无论如何,我在新的`Intent`上保留了`Intent.createChooser()`包装器,让用户选择要浏览的应用程序,并按预期工作.有人可以看到这个解决方案的缺点吗? (4认同)

Mic*_*ł K 38

就像Commonsware提到的那样,你不应该假设你通过的流可以ContentResolver转换成文件.

你真正应该做的是从中打开,然后InputStream从中ContentProvider创建一个Bitmap.它适用于4.4及更早版本,无需反射.

    //cxt -> current context

    InputStream input;
    Bitmap bmp;
    try {
        input = cxt.getContentResolver().openInputStream(fileUri);
        bmp = BitmapFactory.decodeStream(input);
    } catch (FileNotFoundException e1) {

    }
Run Code Online (Sandbox Code Playgroud)

当然,如果你处理大图像,你应该用适当的方法加载它们inSampleSize:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.但这是另一个话题.

  • 多么简单的回答,谢谢!对于跟随此答案的其他人,"cxt"指的是当前的上下文,通常是"this". (2认同)

LEO*_*LEO 32

我相信已经发布的回复应该让人们朝着正确的方向前进.但是,我所做的就是我正在更新的遗留代码.遗留代码使用库中的URI来更改然后保存图像.

在4.4(和谷歌驱动器)之前,URI将如下所示: content:// media/external/images/media/41

正如问题中所述,它们通常看起来像这样: content://com.android.providers.media.documents/document/image:3951

由于我需要能够保存图像而不会干扰现有代码,因此我只是将图库中的URI复制到应用程序的数据文件夹中.然后从数据文件夹中保存的图像文件中生成一个新的URI.

这是个主意:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent), CHOOSE_IMAGE_REQUEST);

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    File tempFile = new File(this.getFilesDir().getAbsolutePath(), "temp_image");

    //Copy URI contents into temporary file.
    try {
        tempFile.createNewFile();
        copyAndClose(this.getContentResolver().openInputStream(data.getData()),new FileOutputStream(tempFile));
    }
    catch (IOException e) {
        //Log Error
    }

    //Now fetch the new URI
    Uri newUri = Uri.fromFile(tempFile);

    /* Use new URI object just like you used to */
 }
Run Code Online (Sandbox Code Playgroud)

注意 - copyAndClose()只执行文件I/O以将InputStream复制到FileOutputStream中.代码未发布.

  • 发布 copyAndClose 的代码,答案不完整 (2认同)

Bri*_*off 22

只是想说这个答案很棒,而且我已经使用了很长时间没有问题.但前段时间我偶然发现了一个问题,即DownloadsProvider以格式返回URI content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fdoc.pdf,因此应用程序崩溃,NumberFormatException因为无法解析其uri段.但是raw:段包含直接uri,可用于检索引用的文件.所以我通过用isDownloadsDocument(uri) if以下内容替换内容来修复它:

final String id = DocumentsContract.getDocumentId(uri);
if (!TextUtils.isEmpty(id)) {
if (id.startsWith("raw:")) {
    return id.replaceFirst("raw:", "");
}
try {
    final Uri contentUri = ContentUris.withAppendedId(
            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
    return getDataColumn(context, contentUri, null, null);
} catch (NumberFormatException e) {
    Log.e("FileUtils", "Downloads provider returned unexpected uri " + uri.toString(), e);
    return null;
}
}
Run Code Online (Sandbox Code Playgroud)

  • 作品完美!谢谢 (2认同)

小智 7

我将多个答案组合成一个使用文件路径的工作解决方案

Mime类型与示例目的无关.

            Intent intent;
            if(Build.VERSION.SDK_INT >= 19){
                intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
                intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
            }else{
                intent = new Intent(Intent.ACTION_GET_CONTENT);
            }
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setType("application/octet-stream");
            if(isAdded()){
                startActivityForResult(intent, RESULT_CODE);
            }
Run Code Online (Sandbox Code Playgroud)

处理结果

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if( requestCode == RESULT_CODE && resultCode == Activity.RESULT_OK) {
        Uri uri = data.getData();
        if (uri != null && !uri.toString().isEmpty()) {
            if(Build.VERSION.SDK_INT >= 19){
                final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
                //noinspection ResourceType
                getActivity().getContentResolver()
                        .takePersistableUriPermission(uri, takeFlags);
            }
            String filePath = FilePickUtils.getSmartFilePath(getActivity(), uri);
            // do what you need with it...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

FilePickUtils

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

public class FilePickUtils {
    private static String getPathDeprecated(Context ctx, Uri uri) {
        if( uri == null ) {
            return null;
        }
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = ctx.getContentResolver().query(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        return uri.getPath();
    }

    public static String getSmartFilePath(Context ctx, Uri uri){

        if (Build.VERSION.SDK_INT < 19) {
            return getPathDeprecated(ctx, uri);
        }
        return  FilePickUtils.getPath(ctx, uri);
    }

    @SuppressLint("NewApi")
    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];
                }

                // TODO handle non-primary volumes
            }
            // 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 getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context The context.
     * @param uri The Uri to query.
     * @param selection (Optional) Filter used in the query.
     * @param selectionArgs (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    public static String getDataColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {
        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }


    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

}
Run Code Online (Sandbox Code Playgroud)


Vas*_*nth 7

如何从URI获取实际文件路径

回答

据我所知,我们不需要从URI获取文件路径,因为对于大多数情况,我们可以直接使用URI来完成我们的工作(例如1.获取位图2.将文件发送到服务器等.)

1.发送到服务器

我们可以使用URI直接将文件发送到服务器.

使用URI我们可以获得InputStream,我们可以使用MultiPartEntity直接将其发送到服务器.

/**
 * Used to form Multi Entity for a URI (URI pointing to some file, which we got from other application).
 *
 * @param uri     URI.
 * @param context Context.
 * @return Multi Part Entity.
 */
public MultipartEntity formMultiPartEntityForUri(final Uri uri, final Context context) {
    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));
    try {
        InputStream inputStream = mContext.getContentResolver().openInputStream(uri);
        if (inputStream != null) {
            ContentBody contentBody = new InputStreamBody(inputStream, getFileNameFromUri(uri, context));
            multipartEntity.addPart("[YOUR_KEY]", contentBody);
        }
    }
    catch (Exception exp) {
        Log.e("TAG", exp.getMessage());
    }
    return multipartEntity;
}

/**
 * Used to get a file name from a URI.
 *
 * @param uri     URI.
 * @param context Context.
 * @return File name from URI.
 */
public String getFileNameFromUri(final Uri uri, final Context context) {

    String fileName = null;
    if (uri != null) {
        // Get file name.
        // File Scheme.
        if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
            File file = new File(uri.getPath());
            fileName = file.getName();
        }
        // Content Scheme.
        else if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
            Cursor returnCursor =
                    context.getContentResolver().query(uri, null, null, null, null);
            if (returnCursor != null && returnCursor.moveToFirst()) {
                int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                fileName = returnCursor.getString(nameIndex);
                returnCursor.close();
            }
        }
    }
    return fileName;
}
Run Code Online (Sandbox Code Playgroud)

2.从URI获取BitMap

如果URI指向图像,那么我们将得到位图,否则为null:

/**
 * Used to create bitmap for the given URI.
 * <p>
 * 1. Convert the given URI to bitmap.
 * 2. Calculate ratio (depending on bitmap size) on how much we need to subSample the original bitmap.
 * 3. Create bitmap bitmap depending on the ration from URI.
 * 4. Reference - http://stackoverflow.com/questions/3879992/how-to-get-bitmap-from-an-uri
 *
 * @param context       Context.
 * @param uri           URI to the file.
 * @param bitmapSize Bitmap size required in PX.
 * @return Bitmap bitmap created for the given URI.
 * @throws IOException
 */
public static Bitmap createBitmapFromUri(final Context context, Uri uri, final int bitmapSize) throws IOException {

    // 1. Convert the given URI to bitmap.
    InputStream input = context.getContentResolver().openInputStream(uri);
    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    onlyBoundsOptions.inDither = true;//optional
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
    input.close();
    if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) {
        return null;
    }

    // 2. Calculate ratio.
    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
    double ratio = (originalSize > bitmapSize) ? (originalSize / bitmapSize) : 1.0;

    // 3. Create bitmap.
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
    bitmapOptions.inDither = true;//optional
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
    input = context.getContentResolver().openInputStream(uri);
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
    input.close();

    return bitmap;
}

/**
 * For Bitmap option inSampleSize - We need to give value in power of two.
 *
 * @param ratio Ratio to be rounded of to power of two.
 * @return Ratio rounded of to nearest power of two.
 */
private static int getPowerOfTwoForSampleRatio(final double ratio) {
    int k = Integer.highestOneBit((int) Math.floor(ratio));
    if (k == 0) return 1;
    else return k;
}
Run Code Online (Sandbox Code Playgroud)

评论

  1. Android没有提供从URI获取文件路径的任何方法,并且在上面的大多数答案中,我们对一些常量进行了硬编码,这可能会破坏功能发布(抱歉,我可能错了).
  2. 在直接从URI获取文件路径的解决方案之前,请尝试使用URI和Android默认方法解决您的用例.

参考

  1. https://developer.android.com/guide/topics/providers/content-provider-basics.html
  2. https://developer.android.com/reference/android/content/ContentResolver.html
  3. https://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/InputStreamBody.html


Mor*_*ard 6

这个Android库处理KitKat中的大小写更改(包括oldere版本 - 2.1+):https:
//github.com/iPaulPro/aFileChooser

使用该String path = FileUtils.getPath(context, uri)命令将返回的Uri转换为可在所有OS版本上使用的路径字符串.在此处查看更多相关信息:https://stackoverflow.com/a/20559175/860488