ManagedQuery(Media.EXTERNAL_CONTENT_URI, 投影,null,null, null);在设备上运行时返回 null

发布于 2024-10-11 13:44:57 字数 242 浏览 3 评论 0原文

String projection[] = new String[]{MediaStore.Images.Media.DATA};
cursor =managedQuery(Media.EXTERNAL_CONTENT_URI, projection,null,null, null);

当我尝试在设备上运行该游标时,该游标返回 null。它在模拟器上运行良好。

有人可以指出我是否遗漏或做错了什么吗?

String projection[] = new String[]{MediaStore.Images.Media.DATA};
cursor =managedQuery(Media.EXTERNAL_CONTENT_URI, projection,null,null, null);

this cursor is returning null when i am trying to run it on device. it works fine on emulator.

Can someone please point out if there is something that i am missing or doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-10-18 13:44:57

此代码的工作原理

public class LoadImagesFromSDCardActivity extends Activity implements
OnItemClickListener {

/**
 * Grid view holding the images.
 **/

private GridView sdcardImages;
/**
 * Image adapter for the grid view.
 **/
private ImageAdapter imageAdapter;
/**
 * Display used for getting the width of the screen. 
 **/
private Display display;
ImageView imgView;

/**
 * Creates the content view, sets up the grid, the adapter, and the click listener.
 * 
 * @see android.app.Activity#onCreate(android.os.Bundle)
 **/
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    // Request progress bar
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.main);

    display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    imgView=(ImageView)findViewById(R.id.imgView);
    setupViews();
    setProgressBarIndeterminateVisibility(true); 
    loadImages();
}

/**
 * Free up bitmap related resources.
 **/
protected void onDestroy() {
    super.onDestroy();
    final GridView grid = sdcardImages;
    final int count = grid.getChildCount();
    ImageView v = null;
    for (int i = 0; i < count; i++) {
        v = (ImageView) grid.getChildAt(i);
        ((BitmapDrawable) v.getDrawable()).setCallback(null);
    }
}
/**
 * Setup the grid view.
 **/
private void setupViews() {
    sdcardImages = (GridView) findViewById(R.id.sdcard);
    sdcardImages.setNumColumns(display.getWidth()/95);
    sdcardImages.setClipToPadding(false);
    sdcardImages.setOnItemClickListener(LoadImagesFromSDCardActivity.this);
    imageAdapter = new ImageAdapter(getApplicationContext()); 
    sdcardImages.setAdapter(imageAdapter);
}
/**
 * Load images.
 */
private void loadImages() {
    final Object data = getLastNonConfigurationInstance();
    if (data == null) {
        new LoadImagesFromSDCard().execute();
    } else {
        final LoadedImage[] photos = (LoadedImage[]) data;
        if (photos.length == 0) {
            new LoadImagesFromSDCard().execute();
        }
        for (LoadedImage photo : photos) {
            addImage(photo);
        }
    }
}
/**
 * Add image(s) to the grid view adapter.
 * 
 * @param value Array of LoadedImages references
 */
private void addImage(LoadedImage... value) {
    for (LoadedImage image : value) {
        imageAdapter.addPhoto(image);
        imageAdapter.notifyDataSetChanged();
    }
}

/**
 * Save bitmap images into a list and return that list. 
 * 
 * @see android.app.Activity#onRetainNonConfigurationInstance()
 */
@Override
public Object onRetainNonConfigurationInstance() {
    final GridView grid = sdcardImages;
    final int count = grid.getChildCount();
    final LoadedImage[] list = new LoadedImage[count];

    for (int i = 0; i < count; i++) {
        final ImageView v = (ImageView) grid.getChildAt(i);
        list[i] = new LoadedImage(((BitmapDrawable) v.getDrawable()).getBitmap());
    }

    return list;
}
/**
 * Async task for loading the images from the SD card. 
 * 
 * @author Mihai Fonoage
 *
 */
class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {

    /**
     * Load images from SD Card in the background, and display each image on the screen. 
     *  
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected Object doInBackground(Object... params) {
        //setProgressBarIndeterminateVisibility(true); 
        Bitmap bitmap = null;
        Bitmap newBitmap = null;
        Uri uri = null;            

        // 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 cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,       
                null); 
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        int size = cursor.getCount();
        // If size is 0, there are no images on the SD Card.
        if (size == 0) {
            //No Images available, post some message to the user
        }
        int imageID = 0;
        for (int i = 0; i < size; i++) {
            cursor.moveToPosition(i);
            imageID = cursor.getInt(columnIndex);
            uri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID);
            try {
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                if (bitmap != null) {
                    newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);
                    bitmap.recycle();
                    if (newBitmap != null) {
                        publishProgress(new LoadedImage(newBitmap));
                    }
                }
            } catch (IOException e) {
                //Error fetching image, try to recover
            }
        }
        cursor.close();
        return null;
    }
    /**
     * Add a new LoadedImage in the images grid.
     *
     * @param value The image.
     */
    @Override
    public void onProgressUpdate(LoadedImage... value) {
        addImage(value);
    }
    /**
     * Set the visibility of the progress bar to false.
     * 
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(Object result) {
        setProgressBarIndeterminateVisibility(false);
    }
}

/**
 * Adapter for our image files. 
 * 
 * @author Mihai Fonoage
 *
 */
class ImageAdapter extends BaseAdapter {

    private Context mContext; 
    private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>();

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

    public void addPhoto(LoadedImage photo) { 
        photos.add(photo); 
    } 

    public int getCount() { 
        return photos.size(); 
    } 

    public Object getItem(int position) { 
        return photos.get(position); 
    } 

    public long getItemId(int position) { 
        return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
        final ImageView imageView; 
        if (convertView == null) { 
            imageView = new ImageView(mContext); 
        } else { 
            imageView = (ImageView) convertView; 
        } 
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(8, 8, 8, 8);
        imageView.setImageBitmap(photos.get(position).getBitmap());
        return imageView; 
    } 
}

/**
 * A LoadedImage contains the Bitmap loaded for the image.
 */
private static class LoadedImage {
    Bitmap mBitmap;

    LoadedImage(Bitmap bitmap) {
        mBitmap = bitmap;
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }
}
/**
 * When an image is clicked, load that image as a puzzle. 
 */
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {        
    int columnIndex = 0;
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection,
            null, 
            null, 
            null);
    if (cursor != null) {
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToPosition(position);
        String imagePath = cursor.getString(columnIndex); 

        FileInputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = new FileInputStream(new File(imagePath));
            bis = new BufferedInputStream(is);
            Bitmap bitmap = BitmapFactory.decodeStream(bis);
            Bitmap useThisBitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
            Log.i("called","called");
            imgView.setImageBitmap(useThisBitmap);
            bitmap.recycle();
            //Display bitmap (useThisBitmap)
        } 
        catch (Exception e) {
            //Try to recover
        }
        finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (is != null) {
                    is.close();
                }
                cursor.close();
                projection = null;
            } catch (Exception e) {
            }
        }
    }
  }

}

main.xml

相同

  <GridView  
    android:id="@+id/sdcard"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth"
    android:gravity="center" />
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imgView"
    ></ImageView>
</LinearLayout>  

same for this code working

public class LoadImagesFromSDCardActivity extends Activity implements
OnItemClickListener {

/**
 * Grid view holding the images.
 **/

private GridView sdcardImages;
/**
 * Image adapter for the grid view.
 **/
private ImageAdapter imageAdapter;
/**
 * Display used for getting the width of the screen. 
 **/
private Display display;
ImageView imgView;

/**
 * Creates the content view, sets up the grid, the adapter, and the click listener.
 * 
 * @see android.app.Activity#onCreate(android.os.Bundle)
 **/
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    // Request progress bar
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.main);

    display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    imgView=(ImageView)findViewById(R.id.imgView);
    setupViews();
    setProgressBarIndeterminateVisibility(true); 
    loadImages();
}

/**
 * Free up bitmap related resources.
 **/
protected void onDestroy() {
    super.onDestroy();
    final GridView grid = sdcardImages;
    final int count = grid.getChildCount();
    ImageView v = null;
    for (int i = 0; i < count; i++) {
        v = (ImageView) grid.getChildAt(i);
        ((BitmapDrawable) v.getDrawable()).setCallback(null);
    }
}
/**
 * Setup the grid view.
 **/
private void setupViews() {
    sdcardImages = (GridView) findViewById(R.id.sdcard);
    sdcardImages.setNumColumns(display.getWidth()/95);
    sdcardImages.setClipToPadding(false);
    sdcardImages.setOnItemClickListener(LoadImagesFromSDCardActivity.this);
    imageAdapter = new ImageAdapter(getApplicationContext()); 
    sdcardImages.setAdapter(imageAdapter);
}
/**
 * Load images.
 */
private void loadImages() {
    final Object data = getLastNonConfigurationInstance();
    if (data == null) {
        new LoadImagesFromSDCard().execute();
    } else {
        final LoadedImage[] photos = (LoadedImage[]) data;
        if (photos.length == 0) {
            new LoadImagesFromSDCard().execute();
        }
        for (LoadedImage photo : photos) {
            addImage(photo);
        }
    }
}
/**
 * Add image(s) to the grid view adapter.
 * 
 * @param value Array of LoadedImages references
 */
private void addImage(LoadedImage... value) {
    for (LoadedImage image : value) {
        imageAdapter.addPhoto(image);
        imageAdapter.notifyDataSetChanged();
    }
}

/**
 * Save bitmap images into a list and return that list. 
 * 
 * @see android.app.Activity#onRetainNonConfigurationInstance()
 */
@Override
public Object onRetainNonConfigurationInstance() {
    final GridView grid = sdcardImages;
    final int count = grid.getChildCount();
    final LoadedImage[] list = new LoadedImage[count];

    for (int i = 0; i < count; i++) {
        final ImageView v = (ImageView) grid.getChildAt(i);
        list[i] = new LoadedImage(((BitmapDrawable) v.getDrawable()).getBitmap());
    }

    return list;
}
/**
 * Async task for loading the images from the SD card. 
 * 
 * @author Mihai Fonoage
 *
 */
class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {

    /**
     * Load images from SD Card in the background, and display each image on the screen. 
     *  
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected Object doInBackground(Object... params) {
        //setProgressBarIndeterminateVisibility(true); 
        Bitmap bitmap = null;
        Bitmap newBitmap = null;
        Uri uri = null;            

        // 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 cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,       
                null); 
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        int size = cursor.getCount();
        // If size is 0, there are no images on the SD Card.
        if (size == 0) {
            //No Images available, post some message to the user
        }
        int imageID = 0;
        for (int i = 0; i < size; i++) {
            cursor.moveToPosition(i);
            imageID = cursor.getInt(columnIndex);
            uri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID);
            try {
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                if (bitmap != null) {
                    newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);
                    bitmap.recycle();
                    if (newBitmap != null) {
                        publishProgress(new LoadedImage(newBitmap));
                    }
                }
            } catch (IOException e) {
                //Error fetching image, try to recover
            }
        }
        cursor.close();
        return null;
    }
    /**
     * Add a new LoadedImage in the images grid.
     *
     * @param value The image.
     */
    @Override
    public void onProgressUpdate(LoadedImage... value) {
        addImage(value);
    }
    /**
     * Set the visibility of the progress bar to false.
     * 
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(Object result) {
        setProgressBarIndeterminateVisibility(false);
    }
}

/**
 * Adapter for our image files. 
 * 
 * @author Mihai Fonoage
 *
 */
class ImageAdapter extends BaseAdapter {

    private Context mContext; 
    private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>();

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

    public void addPhoto(LoadedImage photo) { 
        photos.add(photo); 
    } 

    public int getCount() { 
        return photos.size(); 
    } 

    public Object getItem(int position) { 
        return photos.get(position); 
    } 

    public long getItemId(int position) { 
        return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
        final ImageView imageView; 
        if (convertView == null) { 
            imageView = new ImageView(mContext); 
        } else { 
            imageView = (ImageView) convertView; 
        } 
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(8, 8, 8, 8);
        imageView.setImageBitmap(photos.get(position).getBitmap());
        return imageView; 
    } 
}

/**
 * A LoadedImage contains the Bitmap loaded for the image.
 */
private static class LoadedImage {
    Bitmap mBitmap;

    LoadedImage(Bitmap bitmap) {
        mBitmap = bitmap;
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }
}
/**
 * When an image is clicked, load that image as a puzzle. 
 */
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {        
    int columnIndex = 0;
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection,
            null, 
            null, 
            null);
    if (cursor != null) {
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToPosition(position);
        String imagePath = cursor.getString(columnIndex); 

        FileInputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = new FileInputStream(new File(imagePath));
            bis = new BufferedInputStream(is);
            Bitmap bitmap = BitmapFactory.decodeStream(bis);
            Bitmap useThisBitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
            Log.i("called","called");
            imgView.setImageBitmap(useThisBitmap);
            bitmap.recycle();
            //Display bitmap (useThisBitmap)
        } 
        catch (Exception e) {
            //Try to recover
        }
        finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (is != null) {
                    is.close();
                }
                cursor.close();
                projection = null;
            } catch (Exception e) {
            }
        }
    }
  }

}

And

main.xml

  <GridView  
    android:id="@+id/sdcard"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth"
    android:gravity="center" />
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imgView"
    ></ImageView>
</LinearLayout>  

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文