2014-06-23 45 views
0

我想在android中創建視頻的縮略圖。我使用下面的代碼來創建縮略圖:Android將內容URI轉換爲文件URI

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

其工作正常,當我的URI類型爲文件://但是當我使用URI類型內容://那麼它是不工作。正是給我這個URI類型必須是文件uri的例外。我的班級沒有從活動延伸。所以我不能使用Converting file:// scheme to content:// scheme。有什麼建議我應該如何克服這個問題?或者有什麼方法可以將content://設置爲ThumbnailUtils.createVideoThumbnail

+0

嘗試從URI獲取路徑。 http://stackoverflow.com/a/3414749/2145360 – hayi

回答

-1

你可以調用這個函數一樣,

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

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