0

我正在尝试在 Gridview 教程问题下方的此链接中更改此答案

将特定文件夹中的图像显示到 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 会打印三遍路径。有时我的 Bitmap 内存过大或 Vm 内存已满(这不是我的直接问题) log cat 下面

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

提前致谢..

4

2 回答 2

0

可能是因为你正在使用

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

在 getview... Getview 将运行您在 listview 中有多少项目,并且每当您滚动它时,您的 getview 将再次加载。它像循环一样工作。试着在它之外做你的图像工作。试试看。

于 2012-11-06T06:30:26.577 回答
0

(评论转换为答案)

我猜主要问题(除了OutOfMemory错误)是不正确的getView,它与位置无关。替换paths3为已fileNames[position]修复的。

请注意,您还可以fileNames在 ImageAdapter45 构造函数中检索一次,然后在mThumbIds()getView()

于 2012-11-07T07:39:43.373 回答