简体   繁体   English

Android将内容URI转换为文件URI

[英]Android converting content URI to file URI

I want to create thumbnail of the video in android . 我想在android中创建视频的缩略图。 I am using following code for creating thumbnail : 我正在使用以下代码创建缩略图:

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

Its working fine when my uri type is file:// but when I use uri type content:// then it is not working .It is giving me the exception that URI type must be file uri . 当我的uri类型为file://时,它可以正常工作,但是当我使用uri类型的content://时,它不起作用。这给了我一个例外,即URI类型必须为file uri。 My class in not extended from activity. 我的课程没有从活动中扩展。 So I can't use Converting file:// scheme to content:// scheme . 所以我不能使用将file:// scheme转换为content:// scheme there any suggestion how should I overcome this problem ? 有什么建议我应该如何克服这个问题? or is there any way to give content:// to the ThumbnailUtils.createVideoThumbnail ? 或有什么办法可以给content:// ThumbnailUtils.createVideoThumbnail吗?

You can Call this function like, 您可以像这样调用此函数,

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

decodeUri function is Here, this will decode the Uri and will return the Bitmap. 此处是decodeUri函数,它将解码Uri并返回位图。

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;
    }

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

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