开发者

How to set thumbnail picture for each row in list view with sd card as resource?

开发者 https://www.devze.com 2023-03-22 17:25 出处:网络
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

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 TextV开发者_如何学Pythoniew 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消