2010-12-14 50 views

Trả lời

0

Điều rất quan trọng là phải xác định các thành phần của sự cố. Tôi thực sự muốn nói rằng bạn có hai vấn đề riêng biệt:

  1. Tải xuống tệp từ URL.

  2. Tạo hình thu nhỏ từ tệp video không hoàn chỉnh.

Hãy xem hai cách riêng biệt, bạn sẽ tìm thấy nhiều thông tin.

9

Có thể có được hình thu nhỏ của video bằng ThumbnailUtils.

FileOutputStream out; 
File land=new File(Environment.getExternalStorageDirectory().getAbsoluteFile() 
       +"/portland.jpg");// image file use to create image u can give any path. 
Bitmap bitmap=ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);//filePath is your video file path. 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
     byte[] byteArray = stream.toByteArray(); 

     out=new FileOutputStream(land.getPath()); 
     out.write(byteArray); 
     out.close(); 
+0

Tôi không thể sử dụng dòng này, Nó hiển thị lỗi, File land = new File (Environment.getExternalStorageDirectory(). GetAbsoluteFile() + "/ portland.jpg "); – Anitha

+0

Tôi nhận được hình ảnh từ URL video thì tôi làm gì? .. Hãy giúp tôi. –

+0

Giải pháp duy nhất tốt nhất, vì không giống như MINI_KIND và MICRO_KIND, FULL_SCREEN_KIND chỉ được lưu trữ trên db và không thể truy cập bằng đường dẫn URI. – MPaulo

1

Hãy thử ThumbnailUtils, để có được bitmap video từ đường dẫn tập tin

ThumbnailUtils.createVideoThumbnail(filePath,MediaStore.Video.Thumbnails.MINI_KIND); 
+0

không phải khi bạn đang phát trực tuyến. –

2

Nếu bạn đang đặc biệt yêu cầu về youtube video sau đó tự động 4 tạo ra hình ảnh bằng Youtube.

http://img.youtube.com/vi/video_url_here/0.jpg 
http://img.youtube.com/vi/video_url_here/1.jpg 
http://img.youtube.com/vi/video_url_here/2.jpg 
http://img.youtube.com/vi/video_url_here/3.jpg 

Chuẩn hình ảnh kích thước

chơi nền Thumbnail (480x360 pixel) http://i1.ytimg.com/vi/G0wGs3useV8/0.jpg

Bắt đầu Thumbnail (120x90 pixel) http://i1.ytimg.com/vi/G0wGs3useV8/1.jpg

Trung Thumbnail (120x90 pixel) http://i1.ytimg.com/vi/G0wGs3useV8/2.jpg

End Thumbnail (120x90 pixel) http://i1.ytimg.com/vi/G0wGs3useV8/3.jpg

chất lượng cao Thumbnail (480x360 pixel) http://i1.ytimg.com/vi/G0wGs3useV8/hqdefault.jpg

Medium Chất lượng Thumbnail (320x180 pixel) http://i1.ytimg.com/vi/G0wGs3useV8/mqdefault.jpg

Chất lượng bình thường Thumbnail (120x90 pixel) http://i1.ytimg.com/vi/G0wGs3useV8/default.jpg

Hình thu nhỏ độ phân giải chuẩn (640x480 pixel) http://i1.ytimg.com/vi/G0wGs3useV8/sddefault.jpg

Maximum Nghị quyết Thumbnail (1920x1080 pixels) http://i1.ytimg.com/vi/G0wGs3useV8/maxresdefault.jpg

0

Có thể để có được một hình ảnh thu nhỏ từ một file video hoặc url sử dụng FFMPEG.

FFMPEG phải được tạo bằng cách sử dụng NDK (hoặc bạn có thể tìm thấy một số tệp nhị phân FFMPEG do Android tạo).Dưới đây là một dự án GitHub để xây dựng FFMPEG:

https://github.com/guardianproject/android-ffmpeg

Sau đó bạn có thể bao gồm những chương trình FFMPEG với ứng dụng của bạn và thực hiện FFMPEG từ mã trong ứng dụng của bạn để tạo ra một hình ảnh từ video (địa phương hoặc URL) bằng cách sử dụng lệnh như:

ffmpeg -i videosite.com/video.flv -ss 00:00:15.000 -vframes 1 thumbnail_out.png 

Một điều cần cân nhắc là tìm thời gian (tùy chọn -ss) bạn lấy hình thu nhỏ để trở thành một hình ảnh có ý nghĩa đại diện cho video.

0

Để có được thu nhỏ từ URL, tôi chỉ có một giải pháp cho đến bây giờ, Bạn phải sử dụng This library

Nó Hỗ trợ tập tin, http, https, mms, mmsh và RTMP giao thức

Hỗ trợ aac, acc +, avi, flac, mp2, mp3, mp4, ogg, 3gp và hơn thế nữa! định dạng (âm thanh và video):

1

Có thể tải hình thu nhỏ của video từ url video.

Nếu loại video của bạn là Vimeo theo các bước dưới đây:

1) Giả video Url của bạn là: http://player.vimeo.com/video/17314292

2) Nhận Id video từ Url: ví dụ.

WS 3) Gọi với id video và định dạng XML http://vimeo.com/api/v2/video/17314292.xml

Bạn sẽ nhận được "thumbnail_small", "thumbnail_medium", "thumbnail_large" ở định dạng xml cho ngón tay cái video từ id Video

Nếu loại video của bạn là YouTube làm theo các bước dưới đây:

1) video Url: http://www.youtube.com/embed/Nl2iMF0yKW8

2) Nhận mã video từ url: ví dụ: Nl2iMF0yKW8

3) Thay id video trong url này http://i3.ytimg.com/vi/Nl2iMF0yKW8/default.jpg

LƯU Ý: Chỉ cần thay thế id của bạn ở đây.

1

Nó được làm việc trong trường hợp của tôi

Uri videoUri = data.getData(); 
       String selectedPathVideo=""; 
       selectedPathVideo = ImageFilePath.getPath(getApplicationContext(), videoUri); 
       Log.i("Image File Path", ""+selectedPathVideo); 

       try { 
        Bitmap thumb = ThumbnailUtils.createVideoThumbnail(selectedPathVideo, MediaStore.Video.Thumbnails.MICRO_KIND); 


        imgFarmerVideo.setImageBitmap(thumb); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

Dự án Hỗ trợ file

public class ImageFilePath { 
    /** 
    * Method for return file path of Gallery image 
    * 
    * @param context 
    * @param uri 
    * @return path of the selected image file from gallery 
    */ 
    public static String getPath(final Context context, final Uri uri) 
    { 

     //check here to KITKAT or new version 
     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]; 
       } 
      } 
      // 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()); 
    } 
} 
Các vấn đề liên quan