2012-04-25 41 views
0

大家好, 我已经搜索了很多我的问题,我发现很多帖子都有类似的问题,但没有人给我一个正确的解决方案。 我想要的是一个显示SD卡文件夹图像的gridview。我还必须提供拍摄照片的可能性,并且在返回到gridview时,使用新照片进行更新。在gridview中显示sdcard子文件夹的图像,并在更改时更新

拍照时,我用这个代码:

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

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

就目前而言,我使用游标,让我的图片:

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文件资源管理器相同的行为(当您打开一个文件夹时,这些图片具有所有预览图像,并且它们是异步加载的),我认为它不会使用那些* *缩略图。所以我试图使用位图工厂加载图片为位图,但即使有几张图片(1-2),我立即得到一个“java.lang.OutOfMemoryError:位图大小超过虚拟机预算”...我想我必须调整它们的大小,但是如果我这样做,当我将加载20-30张图片时不会有同样的错误?或者问题是每张照片都超出了预算,所以如果我调整它们的大小,我会避免所有这些错误?

对不起大后,如果有人可以帮助...

回答

0

好吧,我回答自己: 我创建了自己的缩略图是这样的:

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

希望它有帮助..

相关问题