简体   繁体   中英

Android converting content URI to file URI

I want to create thumbnail of the video in 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 . My class in not extended from activity. So I can't use Converting file:// scheme to content:// scheme . there any suggestion how should I overcome this problem ? or is there any way to give content:// to the 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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