【问题标题】:Display sdcard subfolder's images in gridview, and update it when changes在gridview中显示sd卡子文件夹图像,并在更改时更新
【发布时间】:2012-04-25 14:02:41
【问题描述】:

大家好, 我搜索了很多我的问题,我发现了很多类似问题的帖子,但没有人给我一个正确的解决方案。 我想要的是一个显示 sdcard 文件夹图像的网格视图。我还必须提供拍照的可能性,当返回网格视图时,用新图片更新它。

要拍照,我使用以下代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageFileUri());
startActivityForResult(intent, TAKE_PICTURE_ACTIVITY);

getImageFileUri() 是一个函数,它给我一个带有时间戳的图片名称,使用 Environment.getExternalStorageDirectory() 获取 sdcard 路径,并检查文件夹是否存在(如果不存在则创建它)。

目前,我使用光标来获取图像:

private void displayGallery() {

    // Query params :
    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {MediaStore.Images.Media._ID};
    String selection = MediaStore.Images.Media.DATA + " like ? ";
    String[] selectionArgs = {"%Otiama%"};

    // Submit the query :
    mCursor = managedQuery(uri, projection, selection, selectionArgs, null);

    if (mCursor != null) {

        mGridView.setOnItemClickListener(this);
        mGridView.setAdapter(new ImageAdapter(this, mCursor));
    }

    else showToast("Gallery is empty : " + uri.toString());
}

这是我的适配器的 getView :

public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView = new ImageView(mContext);

    // Move cursor to current position
    mCursor.moveToPosition(position);

    // Get the current value for the requested column
    int columnIndex = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    int imageID = mCursor.getInt(columnIndex);

    // obtain the image URI
    Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
    String url = uri.toString();

    // Set the content of the image based on the image URI
    int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
    Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(), originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
    imageView.setImageBitmap(b);

    return imageView;
}

此代码有效,但速度很慢且不会更新(不会创建新图片的缩略图),即使我在 onActivityResult() 中再次调用 displayGallery() 函数也是如此。好吧,即使我重新加载应用程序,它也不会更新 >

事实上,我想要的是和 ES File Explorer 一样的行为(当你打开一个文件夹时,图片都有一个预览图像,并且它们是异步加载的),我认为它不使用那些** 缩略图。

所以我尝试使用位图工厂将图片加载为位图,但即使有几张图片 (1-2),我也会立即收到“java.lang.OutOfMemoryError:位图大小超出 VM 预算”...我我想我必须调整它们的大小,但如果我这样做了,当我加载 20-30 张图片时,我不会有同样的错误吗?或者问题是每张图片都超出了预算,所以如果我调整它们的大小,我会避免所有这些错误?

抱歉这篇大文章,如果有人可以帮助...

【问题讨论】:

    标签: android gridview directory sd-card


    【解决方案1】:

    好吧,我自己回答: 我以这种方式创建了自己的缩略图:

    public static Bitmap resizedBitmap(Bitmap bitmap, int newWidth) {
    
        // Get the bitmap size :
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        double ratio = (double)width / (double)height;
    
        // Compute the thumbnail size :
        int thumbnailHeight = newWidth;
        int thumbnailWidth = (int) ( (double)thumbnailHeight * ratio);
    
        // Create a scaled bitmap :
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, thumbnailWidth, thumbnailHeight, false);
    
        return scaledBitmap;
    }
    

    我不再使用光标,加载缩略图我是这样进行的(在 ImageAdapter 中):

    public void loadThumbnails() {
    
        // Init the ArrayList :
        _thumbnails = new ArrayList<ImageView>();
        _imagesNames = new ArrayList<String>();
    
        // Run through the thumbnails dir :
        File imagesThumbnailsDir = new File(_imagesThumbnailsDirUri.getPath());
        File[] imagesThumbnails = imagesThumbnailsDir.listFiles();
        Arrays.sort(imagesThumbnails);
    
        // For each thumbnail :
        for(File imageThumbnail : imagesThumbnails)
        {
            // Check if the image exists :
            File image = new File(_imagesDirUri.getPath() + File.separator + imageThumbnail.getName());
            if(image.exists()) {
    
                ImageView imageView = new ImageView(_context);
                imageView.setImageDrawable(Drawable.createFromPath(imageThumbnail.getAbsolutePath()));
    
                _thumbnails.add(imageView);
                _imagesNames.add(imageThumbnail.getName());
            }
    
            // If not, delete the thumbnail :
            else {
    
                ImageUtils.deleteFile(Uri.fromFile(imageThumbnail));
            }
        }
    }
    

    所以我的 ImageAdapter 的 getView 函数听起来像这样:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        ImageView imageView;
        if (convertView == null) {
    
            imageView = new ImageView(_context);
            imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setPadding(5, 5, 5, 5);
    
        } else {
            imageView = (ImageView) convertView;
        }
    
        imageView.setImageDrawable(_thumbnails.get(position).getDrawable());
    
        return imageView;
    }
    

    希望对你有帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多