如何在android中搜索sdcard图像?

时间:2011-03-11 15:21:23

标签: android android-sdcard

请问,如何从我的手机SD卡中搜索我的应用中的图像并将路径添加到列表或阵列?

这是我当前的代码,但不会返回任何图像

File images = Environment.getExternalStorageDirectory();  
    File[] imagelist = images.listFiles(new ImageFileFilter());

    for(int x = 0; x < imagelist.length; x++){
        _ImageFiles.add(imagelist[x].getAbsolutePath());
}


public class ImageFileFilter implements FileFilter{

    private final String[] AndroidExtensions = new String[] {"jpg", "png"};

    public boolean accept(File pathname) {
        for (String extension : AndroidExtensions)
        {
          if (pathname.getName().toLowerCase().endsWith(extension))
          {
            return true;
          }
        }
        return false;
    }

}

感谢。

2 个答案:

答案 0 :(得分:3)

 /**
 * Async task for loading the images from the SD card.
 */
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
        }

        // This will remember ACTUAL positions with existing bitmaps!
        mRealBitmapsIndexes = new ArrayList<Integer>();

        int imageID = 0;
        boolean run = true;
        for (int i = 0; i < size; i++) {                
            try {
                if(cursor != null && !cursor.isClosed() && run){ //if the user quits before all the images are loaded, the cursor will be closed 
                    cursor.moveToPosition(i);  
                    imageID = cursor.getInt(columnIndex);
                    uri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID);
                    //temp
                    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

                    if (bitmap != null) {
                        mRealBitmapsIndexes.add(i);
                        newBitmap = Bitmap.createScaledBitmap(bitmap, GRID_IMG_WIDTH, GRID_IMG_HEIGHT, true);
                        bitmap.recycle();
                        if (newBitmap != null) {
                            publishProgress(new LoadedImage(newBitmap));
                        }
                    }
                }
            } catch (OutOfMemoryError e) {
                run = false;
                Toast.makeText(LoadImagesViaGridActivity.this, R.string.lowMemoryErrorUnableDisplImages, Toast.LENGTH_LONG).show();
            }
            catch (Exception e) {
                e.printStackTrace();
                //Toast.makeText(LoadImagesViaGridActivity.this, R.string.errorUnableDisplImages, Toast.LENGTH_LONG).show();
            }
        }
        if(cursor != null && !cursor.isClosed())
            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);
    }
}

答案 1 :(得分:2)

查看类似的问题Load Image from SD Card