Android 画廊不滚动
这是一个由两部分组成的求救请求。我正在创建一个图像库,我将从SD卡上的特定文件夹中提取这些图像。我已创建GalleryView类:Android 画廊不滚动,android,android-gallery,android-cursor,Android,Android Gallery,Android Cursor,这是一个由两部分组成的求救请求。我正在创建一个图像库,我将从SD卡上的特定文件夹中提取这些图像。我已创建GalleryView类: import android.app.Activity; import android.os.Bundle; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
/**
* @author elidd1
*
*/
public class GalleryView extends Activity{
ImageView imageView;
;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Thumbnails._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
MediaStore.Images.Thumbnails.IMAGE_ID);
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new GallImageAdapter(this,cursor,columnIndex));
imageView = (ImageView)findViewById(R.id.ImageView01);
}
}
和一个名为GallImageAdapter的自定义图像适配器:
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GallImageAdapter extends BaseAdapter {
public Cursor cursor;
private int columnIndex;
private Context context;
int imageBackground;
public GallImageAdapter(Context ctx, Cursor cur, int cIn) {
context = ctx;
columnIndex = cIn;
cursor = cur;
}
@Override
public int getCount() {
return cursor.getCount();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if(convertView == null){
picturesView = new ImageView(context);
// move cursor to current position
cursor.moveToPosition(position);
int imageID = cursor.getInt(columnIndex);
picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,""+imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(10,10,10,10);
}else{
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
第一个问题是getCount方法返回空指针异常:
它不允许我滚动浏览图像
问题的第二部分是如何指向特定文件夹。。“/LC/images/”我假设这会发生在我的映像适配器中的这一行:
picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,""+imageID));
感谢您的帮助在使用光标之前,您不必初始化光标,您可以使用光标适配器或在构造函数中传递它对不起,我意识到我忘记在我的gallery视图中启动它,并在发布它后立即将其传递给适配器。。我正在编辑上面的帖子。。仍然想知道如何将特定文件夹传递给适配器..尝试使用Uri.parse('file://your 路径')