1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| public void chooseFile()
{
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
this.myStorage();
}
public void myStorage() {
FirebaseStorage storage = FirebaseStorage.getInstance();
int nbre= (int) (Math.random()*1000);
String val="_"+nbre;
StorageReference storageRef = storage.getReference().child("images/monImage_"+val+".png");
imageView= findViewById(R.id.imageView2);
// Get the data from an ImageView as bytes
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
try {
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = storageRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
}
});
}catch(Exception e)
{System.out.println(e.getMessage());}
} |
Partager