0

Am trying to change this answer in this Link below Gridview Tutorial problems

To display Images from a specific folder unto gridview layout. Am sure i broke it..check out the code and the log cat...

 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

}

The Images print the same images sometimes.. sometimes it does not display, the log.d prints the path three times. Sometimes i have Bitmap memory oversized or Vm memory full(this is not my immediate problem) log cat below

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

Thanks in advance..

Community
  • 1
  • 1
ImGeorge
  • 527
  • 6
  • 24
  • I see no dependency inside the getView to the position parameter so I guess it is kind of logical that you always get the same image? – Marc Van Daele Nov 06 '12 at 10:07
  • hi thanks for replying. can you please give an example of adding dependency inside getview – ImGeorge Nov 06 '12 at 16:44
  • 1
    Isn't it sufficient to use (inside getView) fileNames[position] instead of paths3? – Marc Van Daele Nov 06 '12 at 16:50
  • it worked on my emulator but on the phone it shows out of memory error bitmap size exceed Vm budget.. i will add System.gc.. to see if it works – ImGeorge Nov 07 '12 at 01:25
  • Well still the same thing... the emulator only had two pics maybe that's why it worked.. but the fone has 10 pics.. am updating the code so you see how it is now.. – ImGeorge Nov 07 '12 at 01:32

2 Answers2

0

May be because you are using

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

inside getview... Getview will run how many items you have in listview and whenever you will scroll it you getview will load again. it work like a loop sort of. Try to do you image work outside of it. GIve it a try.

Zohaib Brohi
  • 576
  • 1
  • 7
  • 15
  • Hi thanks for replying....do you how i can circumvent it ..pls show me examples... i will apppreciate.. – ImGeorge Nov 06 '12 at 16:45
0

(comments converted to an answer)

I guess the main problem (apart from the OutOfMemory error) was the incorrect getView which had no dependency towards position. Replacing paths3 with fileNames[position] fixed this.

Note that you can also retrieve fileNames once in the ImageAdapter45 constructor and use it both in mThumbIds() and getView()

Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52