How to show images in list view that are stored in my SD Card particular location

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

I have created an app in which i had use SPenSDK to create image using canvasView. Once i create an 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 stores here "/mnt/sdcard/SmemoExample" in my ListView.

But i am not able to find any solution for that. So please help to solve this out. If possible with an examples.

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;     } } 

}

回答1:

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;     } 


回答2:

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



回答3:

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...



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!