2016-11-16 54 views
0

我一直在試圖看看壓縮​​圖像的其他例子。但是,我仍然不知道在哪裏以及如何將代碼壓縮到。有人能幫助我嗎?上傳到服務器時如何壓縮圖像?

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; 
    } 
+0

它是什麼類型的文件?如果它的JPG或PNG,它已經壓縮。唯一不會是.bmp的文件不太可能。 –

+0

@GabeSechan這是JPG或PNG,但我看到很多例子,雖然當我搜索如何壓縮圖像 – arsenallavigne

+0

這兩個都已經壓縮。如果您重新壓縮JPG,由於jpeg是有損壓縮,將會失去質量。如果你重新壓縮PNG,它會給你完全相同的文件(除非你把它壓縮成jpg,在這種情況下你會失去質量)。如果你對質量損失還可以,你可能會壓縮更多的png,但jpegs已經完成了。一般你不想失去質量。 –

回答

0

下面是壓縮圖像中的位圖

下面代碼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(); 
     } 

嘗試以上解決方案。它會爲我工作。

+0

謝謝!我之前看到過這個,但我不確定我在哪裏放置這段代碼? – arsenallavigne

+0

從廚房或內部存儲器中檢索圖像路徑後。該路徑將採用字符串格式。該圖像使用上述代碼進行壓縮併發送到壓縮圖像。 –

+0

我試過使用上面的方法,但仍然無法解決它。我想我可能錯誤地執行了。 – arsenallavigne