2012-04-25 45 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; 
} 

希望它有幫助..

相關問題