2012-02-27 2 views
0

SD 카드에서 모든 이미지를 가져 와서 격자보기 (단편에 포함)로 표시하려고합니다. 그러나 예외는 발생하지 않지만 gridview에는 아무 것도 표시되지 않고 검은 색 화면이 표시됩니다. 보기의 바인딩 또는 커서로 데이터를 가져 오는 중 어디에 문제가 있는지 잘 모르겠습니다. 이 조각의 현재 코드 :SD 카드에서 이미지 가져 오기 및 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; 
    } 


} 

프로젝트의 레이아웃 파일은 다음과 같습니다

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" /> 

grid_item.xml :

<?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에서

답변

1

을 사용하면 실제로 이미지 뷰의 드로어 블을 아무 것도 설정하지 않습니다. 이미지 경로를 잡고 실제 경로인지 확인한 다음 무시하십시오. 경로를 사용하여 드로어 블을 얻으십시오! 그런 다음 imageView 드로어 블을 해당 이미지로 설정하십시오.

1

이 코드

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

}

시도
관련 문제