我想从FirebaseStorageReference下载所有图像,并将其保存在arraylist中。当我在addOnSuccessListener之外使用位图时,请赋予位图null值。如何在arraylist中使用此位图。
for (int i=0;i<cimage.size();i++) {
StorageReference fileRef = FirebaseStorage.getInstance().getReferenceFromUrl(lang.cimage.get(i));
// https://firebasestorage.googleapis.com/v0/b/app-5dc4e.appspot.com/o/color%2Fblackcar.png?alt=media&token=b1c5c0ec-e640-43c5-bd57-5112c106df94
if (fileRef != null) {
final long ONE_MEGABYTE = 1024 * 1024;
fileRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("bitmap", byteArray);
startActivity(intent);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// progressDialog.dismiss();
Toast.makeText(partques.this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(partques.this, "Upload file before downloading", Toast.LENGTH_LONG).show();
}}
byte[] byteArray = getIntent().getByteArrayExtra("bitmap");
Log.e("Tag", "bitbyte "+byteArray); // byteArray show null
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
答案 0 :(得分:1)
private void uploadImage() {
if(filePath != null)
{
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
StorageReference ref = storageReference.child("images/"+ UUID.randomUUID().toString());
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
Picasso.get().load(taskSnapshot.getUri()).into(imageView);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded "+(int)progress+"%");
}
});
}
}
答案 1 :(得分:0)
尝试一下:
File filename = new File(Environment.getExternalStorageDirectory().getPath(), "myFile.jpeg");
try (FileOutputStream out = new FileOutputStream(filename)) {
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (IOException e) {
e.printStackTrace();
}