我在这些类中使用Bitmap是否有效

时间:2014-06-30 10:31:13

标签: java android multithreading bitmap parse-platform

我正在创建一个应用程序,我可以将图像上传到服务器,但我在这些代码行中遇到问题。它会产生 OutOfMemmoryError ,如下所示。

package com.technow.pereo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;

public class FileHelper {

    public static final String TAG = FileHelper.class.getSimpleName();

    public static final int SHORT_SIDE_TARGET = 1280;

    public static byte[] getByteArrayFromFile(Context context, Uri uri) {
        byte[] fileBytes = null;
        InputStream inStream = null;
        ByteArrayOutputStream outStream = null;

        if (uri.getScheme().equals("content")) {
            try {
                inStream = context.getContentResolver().openInputStream(uri);
                outStream = new ByteArrayOutputStream();

                byte[] bytesFromFile = new byte[1024 * 1024]; // buffer size (1
                                                                // MB)
                int bytesRead = inStream.read(bytesFromFile);
                while (bytesRead != -1) {
                    outStream.write(bytesFromFile, 0, bytesRead);
                    bytesRead = inStream.read(bytesFromFile);
                }

                fileBytes = outStream.toByteArray();
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            } finally {
                try {
                    inStream.close();
                    outStream.close();
                } catch (IOException e) { /* ( Intentionally blank */
                }
            }
        } else {
            try {
                File file = new File(uri.getPath());
                FileInputStream fileInput = new FileInputStream(file);
                fileBytes = IOUtils.toByteArray(fileInput);
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            }
        }

        return fileBytes;
    }

    public static byte[] reduceImageForUpload(byte[] imageData) {
        Bitmap bitmap = null ;
        bitmap = ImageResizer.resizeImageMaintainAspectRatio(imageData,
                SHORT_SIDE_TARGET);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
        bitmap.recycle();
        bitmap = null;
        byte[] reducedData = outputStream.toByteArray();
        try {
            outputStream.close();
        } catch (IOException e) {
            // Intentionally blank
        }


        return reducedData;
    }

    public static String getFileName(Context context, Uri uri, String fileType) {
        String fileName = "uploaded_file.";

        if (fileType.equals(ParseConstants.TYPE_IMAGE)) {
            fileName += "png";
        } else {
            // For video, we want to get the actual file extension
            if (uri.getScheme().equals("content")) {
                // do it using the mime type
                String mimeType = context.getContentResolver().getType(uri);
                int slashIndex = mimeType.indexOf("/");
                String fileExtension = mimeType.substring(slashIndex + 1);
                fileName += fileExtension;
            } else {
                fileName = uri.getLastPathSegment();
            }
        }

        return fileName;
    }

和ImageResizer.class

    package com.technow.pereo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Pair;

public class ImageResizer {

    /*
     * Call this static method to resize an image to a specified width and height.
     * 
     * @param targetWidth  The width to resize to.
     * @param targetHeight The height to resize to.
     * @returns            The resized image as a Bitmap.
     */
    public static Bitmap resizeImage(byte[] imageData, int targetWidth, int targetHeight) {
        // Use BitmapFactory to decode the image
        BitmapFactory.Options options = new BitmapFactory.Options();

        // inSampleSize is used to sample smaller versions of the image
        options.inSampleSize = calculateInSampleSize(options, targetWidth, targetHeight);

        // Decode bitmap with inSampleSize and target dimensions set
        options.inJustDecodeBounds = false; 

        Bitmap reducedBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(reducedBitmap, targetWidth, targetHeight, false);

        return resizedBitmap;        
    }

    public static Bitmap resizeImageMaintainAspectRatio(byte[] imageData, int shorterSideTarget) {
        Pair<Integer, Integer> dimensions = getDimensions(imageData);

        // Determine the aspect ratio (width/height) of the image
        int imageWidth = dimensions.first;
        int imageHeight = dimensions.second;
        float ratio = (float) dimensions.first / dimensions.second;

        int targetWidth;
        int targetHeight;

        // Determine portrait or landscape
        if (imageWidth > imageHeight) {
            // Landscape image. ratio (width/height) is > 1
            targetHeight = shorterSideTarget; 
            targetWidth = Math.round(shorterSideTarget * ratio);
        }
        else {
            // Portrait image. ratio (width/height) is < 1
            targetWidth = shorterSideTarget;
            targetHeight = Math.round(shorterSideTarget / ratio);
        }

        return resizeImage(imageData, targetWidth, targetHeight);
    }

    public static Pair<Integer, Integer> getDimensions(byte[] imageData) {
        // Use BitmapFactory to decode the image
        BitmapFactory.Options options = new BitmapFactory.Options();

        // Only decode the bounds of the image, not the whole image, to get the dimensions
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);

        return new Pair<Integer, Integer>(options.outWidth, options.outHeight);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
}

代码运行时没有错误,但随后抛出OutOfMemmoryException。 我捕获此异常,它最终发送图像,但为什么我收到此错误。这会减慢上传时间。 以下是我在LogCat中获得的详细信息:

06-30 15:59:11.293: W/System.err(9114): java.lang.OutOfMemoryError
06-30 15:59:11.293: W/System.err(9114):     at android.graphics.Bitmap.nativeCreate(Native Method)
06-30 15:59:11.293: W/System.err(9114):     at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
06-30 15:59:11.294: W/System.err(9114):     at android.graphics.Bitmap.createBitmap(Bitmap.java:786)
06-30 15:59:11.294: W/System.err(9114):     at android.graphics.Bitmap.createBitmap(Bitmap.java:718)
06-30 15:59:11.294: W/System.err(9114):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:594)
06-30 15:59:11.294: W/System.err(9114):     at com.technow.pereo.ImageResizer.resizeImage(ImageResizer.java:27)
06-30 15:59:11.294: W/System.err(9114):     at com.technow.pereo.ImageResizer.resizeImageMaintainAspectRatio(ImageResizer.java:55)
06-30 15:59:11.294: W/System.err(9114):     at com.technow.pereo.FileHelper.reduceImageForUpload(FileHelper.java:65)
06-30 15:59:11.294: W/System.err(9114):     at com.technow.pereo.RecipientsActivity.createMessage(RecipientsActivity.java:240)
06-30 15:59:11.300: W/System.err(9114):     at com.technow.pereo.RecipientsActivity$2$1.run(RecipientsActivity.java:96)
06-30 15:59:11.303: W/System.err(9114):     at java.lang.Thread.run(Thread.java:841)

请嗨我!

1 个答案:

答案 0 :(得分:1)

设置要上传到服务器的目标大小或分辨率,并仅使用inSampleSize属性加载基于该目标的图像。

我还建议您以连续方式进行上传而不是并行,这样一次只有一张图像可以在内存中上传。

注意:在您的reduceImageForUpload()中,您应该使用inSampleSize而不是解码图像,然后再回收它。