2012-04-05 190 views
0

大家好我想在我的應用程序中顯示圖像/視頻縮略圖。我使用以下代碼檢索縮略圖並將它們列在適配器中。 以下是代碼如何在android中顯示圖像/視頻縮略圖

public class ImageAdapter extends BaseAdapter { 
     private Context context; 

     private final String[] VideoValues; 

     public ImageAdapter(Context context, String[] VideoValues) { 
      this.context = context; 
      this.VideoValues = VideoValues; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      System.out.println("***********In getView************"); 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View gridView; 

      if (convertView == null) { 

       gridView = new View(context); 

       // get layout from gridlayout.xml 
       gridView = inflater.inflate(R.layout.gridlayout, null); 

       // set value into textview 
       TextView textView = (TextView) gridView 
         .findViewById(R.id.grid_item_label); 
       textView.setText(fileList[position]); 
       System.out.println("value of fileList[position]" + fileList[0]); 
       // set image 
       ImageView imageThumbnail = (ImageView) gridView 
         .findViewById(R.id.grid_item_image); 

       Bitmap bmThumbnail; 

       System.out 
         .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> file path>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" 
           + fileList[position]); 

       bmThumbnail = ThumbnailUtils.createVideoThumbnail(FILE_PATH 
         + fileList[position], 
         MediaStore.Video.Thumbnails.MINI_KIND); 
       if (bmThumbnail != null) { 
        System.out 
          .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 

        imageThumbnail.setImageBitmap(bmThumbnail); 
       } else { 
        System.out 
          .println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>NO THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 

       } 

      } else { 
       gridView = (View) convertView; 
      } 

      return gridView; 
     } 

     public int getCount() { 
      // return 0; 
      return VideoValues.length; 
     } 

     public Object getItem(int position) { 
      return null; 
     } 

     public long getItemId(int position) { 
      return 0; 
     } 

    } 
} 

它的圖像和視頻在三星Galaxy Tab 10.1做工精細,但它不是在此宏碁Iconia A501.any的思想工作。

兩者都在運行HoneyComb。

Rgds,

+1

http://stackoverflow.com/q/9461283/1012284 – 2012-04-05 11:14:34

+0

@Deva冷卻其確定。在4個問題中,他只得到2個答案。他比你好73%。 – 2012-04-05 11:16:29

+0

這只是一個建議。沒有違法,但73%仍然比69%好。 – Deva 2012-04-05 11:25:05

回答

0

我希望這對你有幫助。只要將它調整到您的應用程序:

 View view = convertView; 
     if (convertView == null) 
      view = inflater.inflate(R.layout.grid_item_layout, null); 

     ImageView image = (ImageView) view.findViewById(R.id.image); 
     TextView textView = (TextView) view.findViewById(R.id.date); 

     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 8; 

     String filePath = fileList.get(position).getFilePath(); 
     String dateTaken = fileList.get(position).getDateTaken(); 
     Long type = fileList.get(position).getType(); 
     Bitmap bitmap = null; 
     if(type == MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE) 
      bitmap = BitmapFactory.decodeFile(filePath, options); 
     else if(type == MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO) 
      bitmap = ThumbnailUtils.createVideoThumbnail(filePath, 0); 
     image.setImageBitmap(bitmap); 
     textView.setText(dateTaken);