我想为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卡中添加正确的相应缩略图作为资源。我已经从静态图像中尝试了它但是我无法为动态资源做这件事。谢谢了很多。
答案 0 :(得分:2)
根据我对您的问题的理解,您想从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;
}
}