如何以 SD 卡为资源为列表视图中的每一行设置缩略图?

发布于 2024-11-26 11:09:26 字数 2535 浏览 0 评论 0原文

我想为 SD 卡中的每个相应行设置缩略图。到目前为止,我正在从 SD 卡获取列表视图。这是我的完整代码

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 卡添加正确的相应缩略图作为资源。我已经从静态图像中尝试过,但无法对动态资源执行此操作。非常感谢。

I would like to set a thumbnail picture for each corresponding row from the sd card.As of now I am getting the list view from the sd card.Here is the completed code of mine

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

Please tell me how to add right corresponding thumbnail from sd card as resource.I have tried it from static images but I am unable to do it for dynamic resource.Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半夏半凉 2024-12-03 11:09:26

根据我对你问题的理解,你想从 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;
}
}

According to my understanding of your question, you want to load an image from sdCard, right? If this is the case you can do it like this:

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

Secondly, you can set the thumbnail of each row in ListView by making a custom Adapter by overriding the getView() e.g.

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;
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文