我创建了一个应用程序,其中我使用SPenSDK使用canvasView创建图像。在画布视图中创建图像后,我将图像保存在SD卡“/ mnt / sdcard / SmemoExample”位置。现在我想在我的ListView中显示存储在这里“/ mnt / sdcard / SmemoExample”的所有图像。
但我无法找到任何解决方案。所以请帮助解决这个问题。如果可能,请举例说明。
List_view.xml文件
<?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文件
<?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代码
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;
}
}
}
答案 0 :(得分:6)
答案可供上述问题及其他答案参考。
看看LazyList项目。现在我们对它进行一些修改..
<强> 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);
}
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;
}
答案 1 :(得分:0)
m_Folder = new File("/sdcard/SmemoExample");
&lt; ---删除 mnt
m_Folder = new File(Environment.getExternalSorage()+"SmemoExample");
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD,bOptions);
请参阅this完整示例如何在SD卡中下载和保存图像并从SD卡中显示...
希望这会有用......
答案 2 :(得分:0)
由于您的问题是如何将图像转到列表视图,我假设您可以从SD卡中获取文件。之后你可能想看看这个github项目。 LazyList
另外,通过搜索,我只从这里获得了这个链接。 Lazy load of images in ListView