如何将图像大小从MB减小到KB?

时间:2017-07-31 04:54:08

标签: android android-imageview image-uploading image-resizing

我制作了一个图库应用程序并在SQL Server数据库中存储了更多图像, 现在我得到的图像花了很多时间 所以我想要图像上传时间MB到KB的转换(即:20KB,30KB) 我该怎么办?请帮帮我。

我的代码是:

 private void onSelectFromGalleryResult(Intent data) {

        Bitmap bm = null;
        if (data != null) {
            try {
                bm = MediaStore.Images.Media.getBitmap(Admin.this.getApplicationContext().getContentResolver(), data.getData());
                imgadminview.setImageBitmap(bm);
                int bitmapByteCount= BitmapCompat.getAllocationByteCount(bm);
                System.out.print(bitmapByteCount);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                byteArray = bytes.toByteArray();
               long lengthbmp = byteArray.length;
                System.out.print(lengthbmp);

                if(lengthbmp<=1048576){
                    //KB or more
                    rxKb = lengthbmp/1024;
                    System.out.print(rxKb + " KBs");}
                else if(rxKb>=1024) {
                    //MB or more
                    rxMB = rxKb / 1024;
                    System.out.print(rxMB + " MBs");
                }else if(rxMB>=1024) {
                    //GB or more
                    long rxGB = rxMB / 1024;
                    System.out.print(rxGB + Long.toString(rxGB));
                }else {
                        //rxMB>1024
                    //rxKb > 1024
                }//rxBytes>=1024
                byte[] test=String.valueOf(rxKb).getBytes();
                encodedImage = Base64.encodeToString(test, Base64.DEFAULT);
                btarray = Base64.decode(encodedImage, Base64.DEFAULT);
                bmimage = BitmapFactory.decodeByteArray(byteArray, 0, btarray.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        imgadminview.setImageBitmap(bm);
    }

0 个答案:

没有答案