gpt4 book ai didi

android - 从 Firebase 存储中检索存储的图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:19:20 26 4
gpt4 key购买 nike

我正在尝试使用 Firebase 存储和检索图像。存储图像时没问题,但我无法在 ImageView 中显示它。它没有给出错误,所以我不明白错误在哪里。

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

}
});


}
}
}

同时赋予权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

这是我的 Firebase 存储规则

service firebase.storage {
match /b/fir-app-ce89f.appspot.com/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}

这是我的 Activity 布局

<?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>

最佳答案

我建议看看 FirebaseUI 0.6 ,其中包含一个 Glide 插件,可以让从 Firebase Storage 下载图像变得非常简单:

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

关于android - 从 Firebase 存储中检索存储的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39702304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com