J'essaie de stocker et de récupérer des images à l'aide de Firebase. Cela fonctionne bien lors du stockage d'une image, mais je ne peux pas l'afficher dans un ImageView. Cela ne donne pas d'erreur, donc je ne comprends pas où se trouve l'erreur.
public class MainActivity extends AppCompatActivity {
private Button btnSelectImage;
private ImageView mImageView;
private StorageReference mStorage;
private static final int CAMERA_REQUEST_CODE = 1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSelectImage = (Button) findViewById(R.id.btn_image);
mImageView = (ImageView) findViewById(R.id.imageView);
//Get instance
mStorage = FirebaseStorage.getInstance().getReference();
btnSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_REQUEST_CODE && resultCode==RESULT_OK){
Uri uri = data.getData();
StorageReference filePath = mStorage.child("CameraPhotos").child(uri.getLastPathSegment());
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Show image
Uri downloadUri = taskSnapshot.getDownloadUrl();
Picasso.with(MainActivity.this).load(downloadUri).into(mImageView);
Toast.makeText(MainActivity.this,"Upload Done",Toast.LENGTH_SHORT).show();
Log.v("Test","image load");
}
});
}
}
}
Également donner des autorisations
<uses-permission Android:name="Android.permission.INTERNET" />
<uses-permission Android:name="Android.permission.CAMERA"/>
<uses-permission Android:name="Android.permission.READ_EXTERNAL_STORAGE"/>
Voici mes règles de stockage Firebase
service firebase.storage {
match /b/fir-app-ce89f.appspot.com/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}
Voici la présentation de mon activité
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context="com.example.yusuf.firebaseapp.MainActivity">
<Button
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:text="Select Image"
Android:id="@+id/btn_image"
Android:layout_alignParentTop="true"
Android:layout_centerHorizontal="true" />
<ImageView
Android:layout_width="400dp"
Android:layout_height="350dp"
Android:id="@+id/imageView"
Android:layout_centerVertical="true"
Android:layout_centerHorizontal="true" />
</RelativeLayout>
Je suggère de regarder FirebaseUI 0.6 , qui inclut un plugin Glide permettant de télécharger une image à partir de Firebase Storage aussi simplement que:
// Reference to an image file in Firebase Storage
StorageReference storageReference = ...;
// ImageView in your Activity
ImageView imageView = ...;
// Load the image using Glide
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(storageReference)
.into(imageView);
Je pense que c'est plus facile si vous utilisez cette façon
StorageReference mImageRef =
FirebaseStorage.getInstance().getReference("image/test.jpg");
final long ONE_MEGABYTE = 1024 * 1024;
mImageRef.getBytes(ONE_MEGABYTE)
.addOnSuccessListener(new onSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
imageView.setMinimumHeight(dm.heightPixels);
imageView.setMinimumWidth(dm.widthPixels);
imageView.setImageBitmap(bm);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});