2012-11-09 35 views
6

Tôi đang tạo hình thu nhỏ từ các video được lưu trữ trong thẻ sd của tôi, hiển thị hình thu nhỏ và tên của nó trong chế độ xem lưới. Trên mục sự kiện được chọn của khung nhìn lưới bật lên một hộp thoại và yêu cầu x, y, phải, vị trí dưới cùng sau đó dán nó vào hoạt động chính. Tôi đã nhận các tệp video và cố tạo hình thu nhỏ bằng cách sử dụng cửa hàng phương tiện cũng đang truy xuất hình thu nhỏ dưới dạng bitmap, nhưng bitmap là rỗng. Trong các tên video xem lưới được hiển thị và tôi có thể chọn hình thu nhỏ tương ứng và có thể cho các vị trí cũng có thể đặt hình thu nhỏ thành hoạt động chính. Vấn đề là bitmap là null và hình ảnh bitmap không hiển thị (tên video vie văn bản được hiển thị). Có chuyện gì vậy? Tôi không thể tìm ra nó? Làm ơn giúp tôi? Mã của tôi được đưa ra dưới đây. cảm ơn trước.Video thumbnail return null

 if (f.isFile()) { 
     if (fName.endsWith(".mpg") 
    || fName.endsWith(".mov") 
    || fName.endsWith(".wmv") 
    || fName.endsWith(".rm") 
    || fName.endsWith(".mp4")) { 
    tv.setText(fName); 
    path = f.getAbsolutePath(); 
    System.out.println("Video file path=>"+path); 


thumb = ThumbnailUtils.createVideoThumbnail(f.getAbsolutePath(),MediaStore.Video.Thumbnails.MICRO_KIND); 


    if(thumb==null) 
     { 
     /**Every time it printing null**/ 
     System.out.println("Thumb is null"); 

     } 
     iv.setImageBitmap(thumb); 

Trả lời

3

Từ ThumbnailUtils.createVideoThumbnail tài liệu: May return null if the video is corrupt or the format is not supported.

Theo mặc định, các định dạng được hỗ trợ hầu hết là mp4 và 3gp. Xem tại đây: http://developer.android.com/guide/appendix/media-formats.html để biết danh sách đầy đủ các định dạng phương tiện được hỗ trợ mặc định.

+0

có cách nào để tải hình thu nhỏ từ tệp .mov không? ví dụ: video trên máy chủ wowza? –

+0

Nhưng trong thư viện, tôi có thể xem hình thu nhỏ; - (Vì vậy, video không bị hỏng hoặc bất cứ điều gì –

-1
Try this code. It is getting the thumbnail of videos from urls. instead of pass the path of sd card .it will help you . Dont forgot to add internet permission in manifest file. 
public class VideoThumbnailActivity extends Activity { 

    public static final String Downloader = null; 
    static String uri1="http://daily3gp.com/vids/lucky_guy.3gp"; 
    static String uri2="http://daily3gp.com/vids/reporter_hit_by_plane.3gp"; 
    static String uri3="http://daily3gp.com/vids/motorcycle_wipesout_explodes.3gp"; 
    static String uri4="http://commonsware.com/misc/test2.3gp"; 
    public static String uri_array[]={uri1,uri2,uri3,uri4,uri1,uri2,uri3,uri4,uri1,uri2,uri3,uri4}; 


    ImageView imageView; 
    String url; 
    Gallery ga1,ga2; 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     imageView = (ImageView)findViewById(R.id.imageView); 
     ga1 = (Gallery)findViewById(R.id.gallery1); 
     ga1.setAdapter(new ImageAdapter(getApplicationContext())); 
     imageView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(uri_array[0], MediaStore.Video.Thumbnails.FULL_SCREEN_KIND)); 

     //on click event on gallery 
     ga1.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View view, final int position,long arg3) { 
      imageView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(uri_array[position], MediaStore.Video.Thumbnails.FULL_SCREEN_KIND)); 

      //on click event on imageview to play video 
      imageView.setOnClickListener(new OnClickListener() { 

          @Override 
          public void onClick(View view) { 
           // TODO Auto-generated method stub 
           Intent intent = new Intent(getApplicationContext(),PlayActivity.class); 
           intent.putExtra("path",uri_array[position]); 
           startActivity(intent); 
          } 
         }); 

         } 

       }); 

      } 

      public class ImageAdapter extends BaseAdapter { 

       private Context ctx; 
       int imageBackground; 

       public ImageAdapter(Context c) { 
        ctx = c; 
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1); 
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1); 
        ta.recycle(); 
       } 

       @Override 
       public int getCount() { 

        return uri_array.length; 

       } 

       @Override 
       public Object getItem(int arg0) { 

        return arg0; 
       } 

       @Override 
       public long getItemId(int arg0) { 

        return arg0; 
       } 

       @Override 
       public View getView(int position, View view, ViewGroup arg2) { 

        ImageView iv = new ImageView(ctx); 
        Bitmap curThumb = null; 
        curThumb = ThumbnailUtils.createVideoThumbnail(uri_array[position],MediaStore.Video.Thumbnails.FULL_SCREEN_KIND); 
        iv.setImageBitmap(curThumb); 
        iv.setScaleType(ImageView.ScaleType.FIT_XY); 
        iv.setLayoutParams(new Gallery.LayoutParams(150,120)); 
        iv.setBackgroundResource(imageBackground); 
        return iv; 
      } 
     } 

cho tôi biết vấn đề của bạn được giải quyết hay không.

+0

tôi muốn hình thu nhỏ từ sdcard .. hãy để tôi kiểm tra ... – Sunny

+0

kiểm tra liên kết trả lời dưới đây cho thumnails từ thẻ sd –

+0

ok hãy để tôi kiểm tra .. – Sunny

1

Nếu bạn đang tạo hình thu nhỏ từ video thẻ sd, điều này sẽ tạo ra ThumbnailUtils.createVideoThumbnail nếu không sử dụng con trỏ.

See this example.