从SD卡文件夹加载图像

时间:2012-01-24 05:39:04

标签: android android-sdcard

我正在尝试将sd card.i保存图像中特定文件夹中的图像加载到SD卡中的文件夹中。现在我想从该文件夹加载图像。使用在线教程来完成此操作。但没有任何工作。当我尝试使用下面的代码我收到空白屏幕。 我究竟做错了什么?非常感谢您的帮助。

public class F6Activity extends softActivity
{
private Cursor cursor;
private int columnIndex;
Button next;


protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_f6);


Gallery g = (Gallery) findViewById(R.id.gallery);
//request only the image ID to be returned
String[] projection = {MediaStore.Images.Media._ID};
//Create the cursor pointing to the SDCard
 cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  projection, 
  MediaStore.Images.Media.DATA + " like ? ",
  new String[] {"%ereports%"},  
  null);
  //Get the column index of the image ID
  columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
  g.setAdapter(new ImageAdapter(this));



  }


 private class ImageAdapter extends BaseAdapter {

 private Context context;

 public ImageAdapter(Context localContext) 
 {
  context = localContext;
 }

 public int getCount() 
 {
  return cursor.getCount();
 }
 public Object getItem(int position) 
 {
  return position;
 }
public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 ImageView i = new ImageView(context);
 // Move cursor to current position
 cursor.moveToPosition(position);
 // Get the current value for the requested column
 int imageID = cursor.getInt(columnIndex);
 // obtain the image URI
 Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,      
 Integer.toString(imageID) );
 String url = uri.toString();
 // Set the content of the image based on the image URI
 int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, 
 url.length()));
 Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
 i.setImageBitmap(b);
 i.setLayoutParams(new Gallery.LayoutParams(150, 100));
 i.setScaleType(ImageView.ScaleType.FIT_XY);
 int mGalleryItemBackground = 0;
i.setBackgroundResource(mGalleryItemBackground);

 return i;
 }
 }




 }

1 个答案:

答案 0 :(得分:1)

我对代码感到困惑,现在我不能在代码中说出问题,这对我来说需要时间。我建议使用以下链接提供的代码。它工作正常。之后我会尝试在你的代码中找到问题。现在查看下面的链接。

Display images from SD card