android - 如何在以sd卡为资源的 ListView 中为每一行设置缩略图?

标签 android android-listview thumbnails

我想为 sd 卡中的每个相应行设置一个缩略图。目前我正在从 sd 卡中获取 ListView 。这是我完成的代码

public class SaveList extends ListActivity {

    private List<String> item = null;
    private List<String> path = null;
    private String root="/sdcard/Photos/";
    private TextView myPath;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.savelist);


    }

    private void getDir(String dirPath)
    {
        myPath.setText("Location: " + dirPath);

        item = new ArrayList<String>();
        path = new ArrayList<String>();

        File f = new File(dirPath);
        File[] files = f.listFiles();

        if(!dirPath.equals(root))
        {

            item.add(root);
            path.add(root);

            item.add("../");
            path.add(f.getParent());

        }

        for(int i=0; i < files.length; i++)
        {
            File file = files[i];
            path.add(file.getPath());
            if(file.isDirectory())
                item.add(file.getName() + "/");
            else
                item.add(file.getName());
        }

        ArrayAdapter<String> fileList =
            new ArrayAdapter<String>(this, R.layout.savelistrow, item);
        setListAdapter(fileList);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        File file = new File(path.get(position));

        if (file.isDirectory())
        {
            if(file.canRead())
                getDir(path.get(position));
            else
            {
                new AlertDialog.Builder(this)
                .setIcon(R.drawable.icon)
                .setTitle("[" + file.getName() + "] folder can't be read!")
                .setPositiveButton("OK", 
                        new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).show();
            }
        }
        else
        {
            final String path1= ("/sdcard/Photos/"+file.getName());
            Intent intent = new Intent( getBaseContext(),FullView.class);

            intent.putExtra("link",path1);

            startActivity( intent);
        }
    }
}

请告诉我如何从 sd 卡中添加正确的相应缩略图作为资源。我已经从静态图像中尝试过,但我无法为动态资源这样做。非常感谢。

最佳答案

根据我对你问题的理解,你想从sdCard中加载一张图片,对吧?如果是这种情况,您可以这样做:

ImageView image = (ImageView) findViewById(R.id.imageID);
if(image != null)
{
    Bitmap myBitmap = BitmapFactory.decodeFile("/sdcard/MyImage.jpg");
    if(myBitmap != null)
        image.setImageBitmap(myBitmap);
}

其次,您可以通过重写 getView() 制作自定义适配器,例如设置 ListView 中每一行的缩略图

private class MyCustomAdapter extends ArrayAdapter<String>
{   

public MyCustomAdapter(Context context, int resource, int textViewResourceId, List<String> item) 
{
    super(context, resource, textViewResourceId, item);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View v = convertView;
    if (v == null) 
    {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.my_listview_row, null);
    }  

    ImageView image = (ImageView) v.findViewById(R.id.imageID);
    if(image != null)
    {
        Bitmap myBitmap = BitmapFactory.decodeFile("/sdcard/MyImage.jpg");
        if(myBitmap != null)
            image.setImageBitmap(myBitmap);
    }

    return v;
}
}

关于android - 如何在以sd卡为资源的 ListView 中为每一行设置缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6825112/

相关文章:

android - 如何为 TestClasses 中的 Activity 变量赋值

android - 关闭显示为弹出窗口的 Android Activity

youtube-api - 如何正确使用 oembed 从 youtube 上拉拇指

iphone - 如何在 UITableview 中显示视频缩略图?

android - 带有图像选择和取消选择的 Horizo​​ntalListview

Android缩略图加载问题

android - 安卓手机与智能电视(LG、Sony)收发数据支持DIAL

android - Facebook SDK 3.0 - 获取 Facebook 用户 ID 和访问 token

android - 用surfaceview播放视频时出现问题

android - ListView 的标题 View 在配置更改时销毁