0

I have created an app in which I had use SPenSDK to create image using canvasView. Once I create images in the canvas view, I save the image in the SD Card /mnt/sdcard/SmemoExample location. Now I want to display all the images that are stored here /mnt/sdcard/SmemoExample in my ListView.

But I am not able to find any solution for that.

List_view.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"/>
<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="Clear Cache"/>
</LinearLayout>

item.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
  android:id="@+id/image"
  android:layout_width="50dip"
  android:layout_height="50dip" android:src="@drawable/ic_launcher"   android:scaleType="centerCrop"/>
<TextView
  android:id="@+id/text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1" android:layout_gravity="left|center_vertical" android:textSize="20dip" android:layout_marginLeft="10dip"/>
</LinearLayout>

java code

public class ListActivity extends Activity {
private String[] mFileStrings;

private File[] listFile;

ListView list;

ImageAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
     File file = new File(CanvasActivity.DEFAULT_APP_IMAGEDATA_DIRECTORY);
     
     if (file.isDirectory()) {
         listFile = file.listFiles();
         mFileStrings = new String[listFile.length];

         for (int i = 0; i < listFile.length; i++) {
             mFileStrings[i] = listFile[i].getAbsolutePath();
         }
     }

     list = (ListView) findViewById(R.id.list);
     adapter = new ImageAdapter(this, mFileStrings);
     list.setAdapter(adapter);
}

public class ImageAdapter extends BaseAdapter {
    private Activity activity;
    private String[] data;
    private LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    
    public ImageAdapter(Activity a, String[] d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.length;
    }

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

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);

        TextView text=(TextView)vi.findViewById(R.id.text);;
        ImageView image=(ImageView)vi.findViewById(R.id.image);
        text.setText("item "+position);
        imageLoader.DisplayImage(data[position], image);
        return vi;
    }
}
}
AndroidDev
  • 4,521
  • 24
  • 78
  • 126

3 Answers3

6

Answer is for reference of the above question and other answer.

Just take a look at LazyList project. Now we make a some modification in it..

MainActivity.java

private String[] mFileStrings;
private File[] listFile;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        File file = new File("<Directory path from sdcard>");

        if (file.isDirectory())
        {
            listFile = file.listFiles();
            mFileStrings = new String[listFile.length];

            for (int i = 0; i < listFile.length; i++)
            {
                mFileStrings[i] = listFile[i].getAbsolutePath();
            }
        }

        list = (ListView) findViewById(R.id.list);
        adapter = new LazyAdapter(this, mFileStrings);
        list.setAdapter(adapter);

        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(listener);
    }

in ImageLoader.java

private Bitmap getBitmap(String url)
    {
        File f = new File(url);

        //from SD cache
        Bitmap b = decodeFile(f);
        if (b != null)
        {
            return b;
        }
        return null;
    }
user370305
  • 108,599
  • 23
  • 164
  • 151
  • Remain all code as it is for project, Just replace above mentioned functions only.. – user370305 May 15 '12 at 11:53
  • In my project there is only one difference..jst one activity which is use to save images....in the sd card..and rest all are same – AndroidDev May 15 '12 at 12:23
  • yeh..now crash issue is solve..and it display images but not that are in my sd card...it display the default images that i have set in the imageView background source...if no of image save in my sd card is 5..it display 5 default images.. – AndroidDev May 15 '12 at 12:29
  • Just for understanding purpose, only use LazyList project not include any code of your project just make a change which I mentioned in my answer and give a path of your sdcard image directory, once it will display all images from it, then explore it for how it works and include your code.. – user370305 May 15 '12 at 12:33
  • hey please can you help me here??http://stackoverflow.com/questions/20109034/lazy-list-local-images – Ankit Srivastava Nov 21 '13 at 19:15
  • in my case the file is from media store,i tried using ur code(and i think i understood the logic too) but it didn't help – Ankit Srivastava Nov 21 '13 at 20:23
  • Has it something o do with weakhashmap??used in lazy list? – Ankit Srivastava Nov 21 '13 at 20:39
0

m_Folder = new File("/sdcard/SmemoExample"); <--- remove mnt

m_Folder = new File(Environment.getExternalSorage()+"SmemoExample");

Bitmap bitmap = BitmapFactory.decodeFile(imageInSD,bOptions);

see this complete example how to download and save image in sdcard and display from sdcard...

hope this will work...

MAC
  • 15,799
  • 8
  • 54
  • 95
  • `/mnt/sdcard` is proper location of external storage on some devices. – Sergey Glotov May 15 '12 at 08:47
  • @SergeyGlotov but he want specific location – MAC May 15 '12 at 08:49
  • I mean on some devices path to external storage will be `/mnt/sdcard/`, on some - `/sdcard/`, and God knows what may be the path on the others. [Environment.getExternalStorage()](http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29) should be used. – Sergey Glotov May 15 '12 at 08:53
0

Since your question is how to get images to list view, I assume you can fetch file from sd card. After that you may want to look at this github project. LazyList.

Also, do through search, I got that link from here only. Lazy load of images in ListView

Community
  • 1
  • 1
Master Chief
  • 2,520
  • 19
  • 28