对于循环打印三次或者可能是别的

时间:2012-11-06 06:02:34

标签: java android for-loop android-imageview android-gridview

我试图在下面的链接中更改此答案 Gridview Tutorial problems

将图像从特定文件夹显示到gridview布局。我肯定打破了它..检查代码和日志猫......

 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

提前致谢..

2 个答案:

答案 0 :(得分:0)

可能是因为你正在使用

File path = new File(Environment.getExternalStorageDirectory(),
                "/Crews/Images/");
getview内部... Getview将在listview中运行您有多少项目,每当您滚动它时,getview将再次加载。它像循环一样工作。尝试在外面进行图像处理。试一试。

答案 1 :(得分:0)