Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从SD卡获取图像并在Gridview中显示_Java_Android_Android Sdcard_Android Gridview - Fatal编程技术网

Java 从SD卡获取图像并在Gridview中显示

Java 从SD卡获取图像并在Gridview中显示,java,android,android-sdcard,android-gridview,Java,Android,Android Sdcard,Android Gridview,我正在尝试从SD卡获取所有图像,并将它们显示在gridview中(包含在片段中)。然而,尽管没有抛出异常,gridview中没有显示任何内容,只是一个普通的黑屏。我不确定问题出在哪里,是视图绑定还是将数据提取到游标中。这是片段的当前代码: public class PhotoGridFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { // member variables for

我正在尝试从SD卡获取所有图像,并将它们显示在gridview中(包含在片段中)。然而,尽管没有抛出异常,gridview中没有显示任何内容,只是一个普通的黑屏。我不确定问题出在哪里,是视图绑定还是将数据提取到游标中。这是片段的当前代码:

public class PhotoGridFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

// member variables for
private static final int PHOTO_LIST_LOADER = 0x01;
private ImageCursorAdapter adapter;
private Cursor c;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getLoaderManager().initLoader(PHOTO_LIST_LOADER, null, this);

    adapter = new ImageCursorAdapter(getActivity().getApplicationContext(), c);
}

/* R.layout.grid_item,
null, new String[] { MediaStore.Images.Thumbnails.DATA }, new int[] {R.id.grid_item},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); */

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.photo_item, container, false);     
}


// Loader manager methods
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA };
    CursorLoader cursorLoader = new CursorLoader(getActivity(),
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
            null, null, null);
    return cursorLoader;
}

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    adapter.swapCursor(cursor);

}

public void onLoaderReset(Loader<Cursor> cursor) {
    adapter.swapCursor(null);
}

private class ImageCursorAdapter extends CursorAdapter {

    private LayoutInflater mLayoutInflater;
    private Context mContext;

    public ImageCursorAdapter(Context context, Cursor c) {
        super(context, c);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ImageView newView = (ImageView) view.findViewById(R.layout.grid_item);
        String imagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
        if (imagePath != null && imagePath.length() != 0 && newView != null) {
            newView.setVisibility(ImageView.VISIBLE);
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = mLayoutInflater.inflate(R.layout.grid_item, parent, false);
        return v;
    }


}
公共类PhotoGridFragment扩展片段实现LoaderManager.LoaderCallbacks{
//的成员变量
私有静态最终int照片列表加载程序=0x01;
专用映像游标适配器;
专用游标c;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getLoaderManager().initLoader(照片列表加载器,null,this);
适配器=新的ImageCursorAdapter(getActivity().getApplicationContext(),c);
}
/*R.layout.grid_项目,
null,新字符串[]{MediaStore.Images.Thumbnails.DATA},新int[]{R.id.grid_item},
游标适配器。标志(寄存器、内容、观察者)*/
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
返回充气机。充气(R.layout.photo_项目,容器,假);
}
//装载机管理器方法
公共加载器onCreateLoader(int-id,Bundle-args){
String[]projection={MediaStore.Images.Thumbnails.\u ID,MediaStore.Images.Thumbnails.DATA};
CursorLoader CursorLoader=新的CursorLoader(getActivity(),
MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI,投影,
空,空,空);
返回游标装入器;
}
public void onLoadFinished(加载器,光标){
适配器.swapCursor(游标);
}
公共void onLoaderReset(加载器光标){
适配器.swapCursor(空);
}
私有类ImageCursorAdapter扩展了CursorAdapter{
私人停车场平面布置;
私有上下文;
公共图像游标适配器(上下文,游标c){
超级(上下文,c);
mContext=上下文;
mLayoutInflater=LayoutInflater.from(上下文);
}
@凌驾
公共void bindView(视图、上下文上下文、光标){
ImageView newView=(ImageView)view.findViewById(R.layout.grid_项);
String imagePath=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
if(imagePath!=null&&imagePath.length()!=0&&newView!=null){
设置可见性(ImageView.VISIBLE);
}
}
@凌驾
公共视图newView(上下文上下文、光标、视图组父对象){
视图v=MLAYOUTINGER.充气(R.layout.grid_项,父项,false);
返回v;
}
}
项目的布局文件如下所示:

photo_item.xml:

<?xml version="1.0" encoding="UTF-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photo_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp" />
<?xml version="1.0" encoding="utf-8"?>
<ImageView
 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp"

/>

grid_item.xml:

<?xml version="1.0" encoding="UTF-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photo_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp" />
<?xml version="1.0" encoding="utf-8"?>
<ImageView
 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp"

/>

在bindView中,您实际上没有将imageView的可绘制设置为任何内容。您获取一个图像路径,验证它是否为真实路径,然后忽略它:)使用该路径获取可绘制的图像!然后将imageView的可绘制设置为该图像。

尝试以下代码

sdcard.java

public class Sdcard extends Activity {

//  Cursor used to access the results from querying for images on the SD card.

private Cursor cursor;

 // Column index for the Thumbnails Image IDs.

private int columnIndex;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sdcard);

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

    GridView sdcardImages = (GridView) findViewById(R.id.gridView1);
    sdcardImages.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;
    }
    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);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // Set the content of the image based on the provided URI
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
            picturesView.setPadding(10, 10, 10, 10);
            picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
        }
        else {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}
}