方法不返回位图

时间:2015-08-17 18:45:27

标签: android bitmap

我有这两个方法,downloadBitmapImage和readBitmapImage。第一个从URL中提取图像,将其本地存储在SD卡上,第二个将图像作为位图返回以供稍后使用。为什么位图继续返回为null?

    void downloadBitmapImage() {
        InputStream input;
        try {
            URL url = new URL (strURL);
            input = url.openStream();
            byte[] buffer = new byte[1500];
            OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
            try {
                int bytesRead = 0;
                while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                    output.write(buffer, 0, bytesRead);
                }
            }
            finally {
                output.close();
                buffer = null;
            }
        }
        catch(Exception e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    Bitmap readBitmapImage() {

        BitmapFactory.Options bOptions = new BitmapFactory.Options();
        bOptions.inTempStorage = new byte[16*1024];

        File file = null;
        FileOutputStream fos = null;

        try
        {
            file = new File(getExternalFilesDir(null), "/sdcard/"+pos+".png");
            fos = new FileOutputStream(file);
        }
        catch(FileNotFoundException e)
        {
            e.getMessage();
        }
        String imageInSD = file.getAbsolutePath().toString();

        bitmap = BitmapFactory.decodeFile(imageInSD,bOptions);
        return bitmap;
    }

1 个答案:

答案 0 :(得分:0)

我在代码中使用了以下方法。

/**
 * Download image from server url and return bitmap
 *
 * @param stringUrl Imaage download url
 * @return Bitmap receieved from server
 */
public static Bitmap downloadImage(String stringUrl) {
    URL url;
    Bitmap bm = null;
    try {
        url = new URL(stringUrl);
        URLConnection ucon = url.openConnection();
        InputStream is;
        if (ucon instanceof HttpURLConnection) {
            HttpURLConnection httpConn = (HttpURLConnection) ucon;
            int statusCode = httpConn.getResponseCode();
            if (statusCode == 200) {
                is = httpConn.getInputStream();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                BufferedInputStream bis = new BufferedInputStream(is, 8192);
                ByteArrayBuffer baf = new ByteArrayBuffer(1024);
                int current = 0;
                while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
                }
                byte[] rawImage = baf.toByteArray();
                bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
                bis.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bm;
}






 /**
     * Decode bitmap from internal storage having specific width and height
     *
     * @param filePath  path of image to be decode
     * @param reqWidth  width of bitmap
     * @param reqHeight hieght of bitmap
     * @return bitmap from given path
     */
    public static Bitmap decodeScaledBitmapFromSdCard(String filePath, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    /**
     * Reduce quality of bitmap for smaller images
     *
     * @param options   various options for BitmapFactory
     * @param reqWidth  width of bitmap
     * @param reqHeight hight of bitmap
     * @return lower resolution bitmap
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

根据您的需要使用此方法。

我希望它有所帮助!