2016-11-19 5 views
1

Ich habe eine App, mit der der Benutzer ein Ereignis erstellen und ein Bild aus seiner Galerie hochladen und in FirebaseStorage hochladen kann. Alles funktioniert gut, aber die Bilder sind wirklich groß und ich möchte den Lade-/Download-Prozess optimieren. Ich habe bereits mit dieser versucht:Bildkomprimierung vor dem Hochladen auf FirebaseStorage

// Get the data from an ImageView as bytes 
imageView.setDrawingCacheEnabled(true); 
imageView.buildDrawingCache(); 
Bitmap bitmap = imageView.getDrawingCache(); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
byte[] data = baos.toByteArray(); 

UploadTask uploadTask = mountainsRef.putBytes(data); 
uploadTask.addOnFailureListener(new OnFailureListener() { 
@Override 
public void onFailure(@NonNull Exception exception) { 
    // Handle unsuccessful uploads 
} 
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
@Override 
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
    // taskSnapshot.getMetadata() contains file metadata such as size, content-type 
    Uri downloadUrl = taskSnapshot.getDownloadUrl(); 
} 
}); 

und Einstellung bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos); Das funktioniert aber das macht im Grunde jedes Bild hässlich. Gibt es einen Weg, wenn möglich, mit einer Anleitung, die Größe des Bildes zu reduzieren, ohne die Qualität vollständig zu zerstören?

Antwort

1

Was mir geholfen, ich war abgespeckte die Bildauflösung nach das aufgenommene Bild zu 1280 x 720 konvertieren und dann ging es meine Dateigröße um mehr als 50%.

Hier ist eine Beispielmethode.

/** 
    * Compress the file and return the compressed photo 
    * 
    * @return photoFile 
    */ 
    public File PhotoCompressor(File PhotoFile) { 
     Bitmap b = BitmapFactory.decodeFile(photoFile.getAbsolutePath()); 


     int originalWidth = b.getWidth(); 
     int originalHeight = b.getHeight(); 
     int boundWidth = COMPRESSED_PHOTO_WIDTH; 
     int boundHeight = COMPRESSED_PHOTO_HEIGHT; 
     int newWidth = originalWidth; 
     int newHeight = originalHeight; 

     //check if the image needs to be scale width 
     if (originalWidth > boundWidth) { 
      //scale width to fit 
      newWidth = boundWidth; 
      //scale height to maintain aspect ratio 
      newHeight = (newWidth * originalHeight)/originalWidth; 
     } 

     //now check if we need to scale even the new height 
     if (newHeight > boundHeight) { 
      //scale height to fit instead 
      newHeight = boundHeight; 
      //scale width to maintain aspect ratio 
      newWidth = (newHeight * originalWidth)/originalHeight; 
     } 
     Log.i(TAG, "Original Image:" + originalHeight + " x" + originalWidth); 
     Log.i(TAG, "New Image:" + newHeight + " x" + newWidth); 
     try { 
      Bitmap out = Bitmap.createScaledBitmap(b, newWidth, newHeight, true); 
      FileOutputStream fOut; 
      fOut = new FileOutputStream(photoFile); 
      out.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
      fOut.flush(); 
      fOut.close(); 
      b.recycle(); 
      out.recycle(); 
     } catch (OutOfMemoryError exception) { 
      Log.e(TAG, "OutofMemory excpetion" + exception); 
      exception.printStackTrace(); 
     } catch (FileNotFoundException e) { 
      Log.e(TAG, "File not found excpetion" + e); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      Log.e(TAG, "IO exception excpetion" + e); 
      e.printStackTrace(); 
     } 
     return photoFile; 
    } 
Verwandte Themen