1

请问,如何在我的手机 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;
    }

}

谢谢。

4

1 回答 1

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);
    }
}
于 2011-03-11T15:34:02.680 回答