2012-11-06 42 views
0

我試圖在這個鏈接更改以下 Gridview Tutorial problemsFor循環打印三次或可能是別的東西

這個答案要對gridview的佈局特定的文件夾顯示圖像。相信我爆發it..check出代碼和日誌貓...

package nsixty.crew.app; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Environment; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class ImageAdapter45 extends BaseAdapter { 

    private Context mContext; 


    public ImageAdapter45(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return mThumbIds(); 
    } 

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

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

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some 
            // attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

//  File path = new File(Environment.getExternalStorageDirectory(),"/Crews/Images/"); 
//  String paths = null; 
//  
//  if(path.exists()) 
//  { 
//   
//  
//  
//  
//  String[] fileNames = path.list(); 

     //Log.d("I be mumu", data); 

     ///after you use your images 
//  System.gc(); 
     //Bitmap mBitmap = null; 
     //for(int i = 0; i < fileNames.length; i++) 
     //{ 
//   Boolean finish = true; 
//   
       //paths = (path.getPath() + "/" + fileNames[i]); 
//    BitmapFactory.Options opts=new BitmapFactory.Options(); 
//    opts.inDither=false;      //Disable Dithering mode 
//    opts.inPurgeable=true;   
//    opts.inPurgeable=true;     //Tell to gc that whether it needs free memory, the Bitmap can be cleared 
//    opts.inInputShareable=true;    //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future 
//    opts.inTempStorage=new byte[32 * 1024]; 

        //System.gc(); 

     File path = new File(Environment.getExternalStorageDirectory(), 
       "/Crews/Images/"); 
     String paths = null; 
     // String[] fileNames = null ; 

     StringBuilder sb = new StringBuilder(); 

     if (path.exists()) { 
      //ArrayList<String> urls = new ArrayList<String>(); 
      String[] fileNames = path.list(); 
      //List<String> urls =(Arrays.asList(fileNames)); 

//   for (int i = 0; i < fileNames.length; i++) { 
////    sb.append((path.getPath() + "/" + fileNames[i]) + "\n"); 
//    paths = ((path.getPath() + "/" + fileNames[i])); 
//    urls.add(paths); 
//    
//    //Log.v("YELLLO12",urls.toArray().toString()); 
//    
//    
//    
//   } 
//   urls.add(paths); 

//   String paths3 = ""; 
// 
//   for (String s : urls) 
//   { 
//    paths3 += s + "\n"; 
//    
//    // imageView.setImageBitmap(BitmapFactory.decodeFile((path.getPath() + "/" + paths3))); 
//   } 
      imageView.setImageBitmap(BitmapFactory.decodeFile((path.getPath() + "/" +fileNames[position]))); 
      System.gc(); 
      Log.v("YELLLO1237P",fileNames[position]); 

      //imageView.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/Crews/Images/IMG_06_11_2012_01_16_05.jpg")); 
      //Log.v("YELLLO300",path.getPath() + "/" +paths3); 

      //Bitmap mBitmap = BitmapFactory.decodeFile(paths3); 
      //imageView.setImageBitmap(mBitmap); 
      //Log.v("YELLLO123",paths); 
      //Log.v("YELLLO124ggh",fileNames.toString()); 


     } 
     //path.close(); 


       // } 

      //fos.close(); 








     return imageView; 
    } 
    //static String data = ImageAdapter45.data3(); 


    //static String[] mThumbIds = {data}; 
// public static String Pathss(){ 
//  File path = new File(Environment.getExternalStorageDirectory(),"/Crews/Images/"); 
//  String paths = null; 
//  String[] fileNames = null ; 
//  
//  StringBuilder sb = new StringBuilder(); 
//  if(path.exists()) 
//  { 
//   fileNames = path.list(); 
//   
//   for(int i = 0; i < fileNames.length; i++) 
//   { 
//    sb.append((path.getPath()+"/"+ fileNames[i])); 
//    paths = sb.toString(); 
//   } 
//   
//  } 
//  return paths.trim(); 
// } 

    public Integer mThumbIds() { 
     File path = new File(Environment.getExternalStorageDirectory(),"/Crews/Images/"); 
     //String paths = null; 
     String[] fileNames = null ; 
     if(path.exists()) 
     { 
      fileNames = path.list(); 


     } 





     return fileNames.length; 

     //Log.d("I be mumu", data); 

//  StringBuilder sb = new StringBuilder(); 
//  for(int i = 0; i < fileNames.length; i++) 
//  { 
//   //Boolean finish = true; 
//   //if (finish == true){ 
//    String paths = (path.getPath() + "/" + fileNames[i]); 
//    sb.append 
//    //Log.d("o men see fuckup", paths); 
//    
//    // } 
//   //fos.close(); 
//   
//  
//  } 
//  




    } 


    // references to our images 

} 

的圖像打印相同的圖像有時..有時不顯示,該log.d打印路徑三倍。有時候,我有位圖的內存過大或VM內存滿(這不是我的眼前的問題)低於事先

11-06 05:58:43.282: I/dalvikvm(1474): Could not find method android.widget.LinearLayout.setAlpha, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineLinearLayout.setAlpha 
11-06 05:58:43.282: W/dalvikvm(1474): VFY: unable to resolve virtual method 3671: Landroid/widget/LinearLayout;.setAlpha (F)V 
11-06 05:58:43.282: D/dalvikvm(1474): VFY: replacing opcode 0x6f at 0x000a 
11-06 05:58:43.282: D/dalvikvm(1474): VFY: dead code 0x000d-000d in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineLinearLayout;.setAlpha (F)V 
11-06 05:58:43.282: I/dalvikvm(1474): Could not find method android.widget.LinearLayout.setTranslationX, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineLinearLayout.setTranslationX 
11-06 05:58:43.282: W/dalvikvm(1474): VFY: unable to resolve virtual method 3677: Landroid/widget/LinearLayout;.setTranslationX (F)V 
11-06 05:58:43.282: D/dalvikvm(1474): VFY: replacing opcode 0x6f at 0x000a 
11-06 05:58:43.282: D/dalvikvm(1474): VFY: dead code 0x000d-000d in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineLinearLayout;.setTranslationX (F)V 
11-06 05:58:43.392: W/KeyCharacterMap(1474): No keyboard for id -1 
11-06 05:58:43.402: W/KeyCharacterMap(1474): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
11-06 05:58:43.462: I/dalvikvm(1474): Could not find method com.actionbarsherlock.internal.view.menu.ActionMenuItemView.onHoverEvent, referenced from method com.actionbarsherlock.internal.view.menu.ActionMenuItemView.dispatchHoverEvent 
11-06 05:58:43.462: W/dalvikvm(1474): VFY: unable to resolve virtual method 5030: Lcom/actionbarsherlock/internal/view/menu/ActionMenuItemView;.onHoverEvent (Landroid/view/MotionEvent;)Z 
11-06 05:58:43.462: D/dalvikvm(1474): VFY: replacing opcode 0x6e at 0x0006 
11-06 05:58:43.462: D/dalvikvm(1474): VFY: dead code 0x0009-0009 in Lcom/actionbarsherlock/internal/view/menu/ActionMenuItemView;.dispatchHoverEvent (Landroid/view/MotionEvent;)Z 
11-06 05:58:43.502: I/dalvikvm(1474): Could not find method android.widget.LinearLayout.onPopulateAccessibilityEvent, referenced from method com.actionbarsherlock.internal.view.menu.ActionMenuItemView.onPopulateAccessibilityEvent 
11-06 05:58:43.502: W/dalvikvm(1474): VFY: unable to resolve virtual method 3667: Landroid/widget/LinearLayout;.onPopulateAccessibilityEvent (Landroid/view/accessibility/AccessibilityEvent;)V 
11-06 05:58:43.502: D/dalvikvm(1474): VFY: replacing opcode 0x6f at 0x0006 
11-06 05:58:44.163: D/dalvikvm(402): GC_EXPLICIT freed 2K, 54% free 2538K/5511K, external 410K/517K, paused 13074ms 
11-06 05:58:44.231: I/ActivityManager(61): Displayed nsixty.crew.app/.ViewPagerFragment: +8s985ms 
11-06 05:58:53.015: D/SntpClient(61): request time failed: java.net.SocketException: Address family not supported by protocol 
11-06 05:58:53.692: D/dalvikvm(140): GC_EXPLICIT freed 101K, 47% free 3193K/6023K, external 1597K/1823K, paused 2707ms 
11-06 05:59:26.372: I/ActivityManager(61): Starting: Intent { act=nsixty.crew.app.GridView34 cmp=nsixty.crew.app/.GridView34 } from pid 1474 
11-06 05:59:27.082: V/YELLLO300(1474): /mnt/sdcard/Crews/Images/IMG_06_11_2012_01_16_05.jpg 
11-06 05:59:27.082: V/YELLLO300(1474): IMG10_10_2012_01_55_03.jpg 
11-06 05:59:27.221: V/YELLLO300(1474): /mnt/sdcard/Crews/Images/IMG_06_11_2012_01_16_05.jpg 
11-06 05:59:27.221: V/YELLLO300(1474): IMG10_10_2012_01_55_03.jpg 
11-06 05:59:27.241: V/YELLLO300(1474): /mnt/sdcard/Crews/Images/IMG_06_11_2012_01_16_05.jpg 
11-06 05:59:27.241: V/YELLLO300(1474): IMG10_10_2012_01_55_03.jpg 
11-06 05:59:27.291: V/YELLLO300(1474): /mnt/sdcard/Crews/Images/IMG_06_11_2012_01_16_05.jpg 
11-06 05:59:27.291: V/YELLLO300(1474): IMG10_10_2012_01_55_03.jpg 
11-06 05:59:27.962: I/ActivityManager(61): Displayed nsixty.crew.app/.GridView34: +1s395ms 

感謝 日誌貓..

+0

我看到getView內沒有依賴於位置參數,所以我想這是一種合乎邏輯的,你總能得到相同的圖像? –

+0

嗨,感謝您的回覆。你可以請給添加依賴內部getview – ImGeorge

+1

是不是足夠使用(內getView)文件名[位置]代替paths3的例子嗎? –

回答

0

(註釋轉換爲答案)

我想主要的問題(除了OutOfMemory錯誤)是對位置沒有依賴性的不正確的getView。用fileNames[position]代替paths3解決了這個問題。

請注意,您還可以在ImageAdapter45構造檢索fileNames一次,並用它無論是在mThumbIds()getView()

0

可能是因爲您正在使用

File path = new File(Environment.getExternalStorageDirectory(), 
       "/Crews/Images/"); 

裏面的getview ... Getview將運行你在listview中有多少物品,每當你滾動它時,getview將再次加載。它的工作方式類似於循環。試着做你的形象工作。試一試。

+0

嗨謝謝你回答....你怎麼我可以規避它..pls顯示我例子...我會apppreciate .. – ImGeorge