Android将内容URI转换为文件URI

时间:2014-06-23 03:29:34

标签: java android

我想在android中创建视频的缩略图。我使用以下代码创建缩略图:

    String path =  (new File(URI.create(url))).getAbsolutePath();
    Log.d("path",path);
    bitmap =  ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MICRO_KIND);

当我的uri类型为 file:// 时,它正常工作但是当我使用uri类型 content:// 时,它无效。它正在给我URI类型必须是文件uri的例外。我的班级没有延伸到活动。所以我不能使用Converting file:// scheme to content:// scheme。有什么建议我应该如何克服这个问题?或者有没有办法将content://提供给ThumbnailUtils.createVideoThumbnail

1 个答案:

答案 0 :(得分:-1)

您可以将此功能称为

Uri selectedImage = data.getData();
Bitmap b = decodeUri(selectedImage);

decodeUri函数在这里,这将解码Uri并返回Bitmap。

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

         Bitmap resizedBitmap = null;
         errSmallImage = false;

         BitmapFactory.Options o = new BitmapFactory.Options();
         o.inJustDecodeBounds = true;
         BitmapFactory.decodeStream( getContentResolver().openInputStream(selectedImage), null, o);
         final int REQUIRED_SIZE = 100;
         int width_tmp = o.outWidth, height_tmp = o.outHeight;

         if(width_tmp >=300 || height_tmp >=300 ){

             System.out.println("decodeUri : Original Resolution : , "+width_tmp+"x"+height_tmp);

             int scale = 1;
             while (true) {
                 if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                     break;
                 }
                 width_tmp /= 2;
                 height_tmp /= 2;
                 scale *= 2;
             }

             BitmapFactory.Options o2 = new BitmapFactory.Options();
             o2.inSampleSize = scale;
             //return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
             Bitmap b = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
             Matrix matrix = new Matrix();
             float rotation = rotationForImage(context, selectedImage);
             if (rotation != 0f) {
                   matrix.preRotate(rotation);
              }
             resizedBitmap = Bitmap.createBitmap(b, 0, 0, width_tmp, height_tmp, matrix, true);
         }else{
             errSmallImage=true;
             resizedBitmap = null;
         }
         return resizedBitmap;
     }
        public static float rotationForImage(Context context, Uri uri) {
            if (uri.getScheme().equals("content")) {
            String[] projection = { Images.ImageColumns.ORIENTATION };
            Cursor c = context.getContentResolver().query(
                    uri, projection, null, null, null);
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } else if (uri.getScheme().equals("file")) {
            try {
                ExifInterface exif = new ExifInterface(uri.getPath());
                int rotation = (int)exifOrientationToDegrees(
                        exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL));
                return rotation;
            } catch (IOException e) {
                Log.e("Photo Import", "Error checking exif", e);
            }
        }
            return 0f;
        }
        private static float exifOrientationToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }
        return 0;
    }