簡體   English   中英

上傳到服務器時如何壓縮圖像?

[英]How do I compress image when uploading to server?

我一直在嘗試查看圖像壓縮的其他示例。 但是,我仍然不知道在哪里以及如何包含壓縮代碼。 有人可以幫我嗎?

public void uploadMultipart() {
            //getting name for the image
            String name = editText.getText().toString().trim();

        //getting the actual path of the image
        String path = getPath(filePath);


        //Uploading code
        try {
            String uploadId = UUID.randomUUID().toString();

            //Creating a multi part request
            new MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL)
                    .addFileToUpload(path, "image") //Adding file
                    .addParameter("name", name) //Adding text parameter to the request
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload(); //Starting the upload

        } catch (Exception exc) {
            Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

 //method to get the file path from uri
    public String getPath(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        String document_id = cursor.getString(0);
        document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
        cursor.close();

        cursor = getContentResolver().query(
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
        cursor.moveToFirst();

        String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
//        Bitmap bmp = BitmapFactory.decodeFile(path);
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        cursor.close();


        return path;
    }

這是在位圖中壓縮圖像的代碼

jpeg圖片的以下代碼

   Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("imagename.png"));
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); // you can set as 90 for compress ration
   Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

下面的png圖片代碼:

   Bitmap bitmap= BitmapFactory.decodeStream(getAssets().open("imagename.png"));
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
   Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

否則,這里是編碼字符串並以編碼格式圖像發送到服務器的代碼

  String encodedString ="";
  try {

            BitmapFactory.Options options = null;
            options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            bitmap = BitmapFactory.decodeFile(filepath, options);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Must compress the Image to reduce image size to make upload easy
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);

            byte[] byte_arr = stream.toByteArray();
            // Encode Image to String
            encodedString = Base64.encodeToString(byte_arr, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

嘗試上述解決方案。 它將為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM