2016-11-16 11 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を圧縮することができるかもしれませんが、jpegはすでに完了しています。そして、一般的に品質を失うことは望ましくありません。 –

答えて

0

ここでビットマップ

JPEG画像、PNG画像のためのコードの下

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())); 

ためのコードの下

で圧縮画像のコードされた:

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

関連する問題