Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 压缩bitmat时出错_Android_Bitmap_Android Bitmap_Bitmapfactory - Fatal编程技术网

Android 压缩bitmat时出错

Android 压缩bitmat时出错,android,bitmap,android-bitmap,bitmapfactory,Android,Bitmap,Android Bitmap,Bitmapfactory,我试图压缩位图,但得到一个空指针异常。logcat显示未捕获异常 Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes , 0, imageAsBytes .length); Bitmap bPNGcompress =codec(bitmap, Bitmap.CompressFormat.PNG, 0); Bitmap scaled = bPNGcompress.

我试图压缩位图,但得到一个空指针异常。logcat显示未捕获异常

 Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes , 0, imageAsBytes .length);
                Bitmap bPNGcompress =codec(bitmap, Bitmap.CompressFormat.PNG, 0);


                Bitmap scaled = bPNGcompress.createScaledBitmap( bPNGcompress, 100, 100, true );
方法实现

  private static Bitmap codec(Bitmap map, Bitmap.CompressFormat format,
        int quality) {

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    map.compress(format, quality, os);
    try {
        os.flush();
        os.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte[] array = os.toByteArray();
    return BitmapFactory.decodeByteArray(array, 0, array.length);
}
请试试这个

 private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,int quality) 
   {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    src.compress(format, quality, os);

    byte[] array = os.toByteArray();
    return BitmapFactory.decodeByteArray(array, 0, array.length);
}

private static class SampleView extends View {

    // CONSTRUCTOR
    public SampleView(Context context) {
        super(context);
        setFocusable(true);

    }
    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();

        canvas.drawColor(Color.GRAY);

                    //  you need to insert some image flower_blue into res/drawable folder
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.flower_blue);
                    // Best of quality is 80 and more, 3 is very low quality of image 
        Bitmap bJPGcompress = codec(b, Bitmap.CompressFormat.JPEG, 3);
               // get dimension of bitmap getHeight()  getWidth()
       int h = b.getHeight();

       canvas.drawBitmap(b, 10,10, paint);
       canvas.drawBitmap(bJPGcompress, 10,10 + h + 10, paint);

    }

}