我正在开发一个应用程序,我试图将我的图像添加到实时数据库中的存储和图像URL中,但当我试图上传时,它说失败了。我不明白为什么会发生这种情况。请调查一下,并指导我为什么会发生这种情况。我是新来的安卓系统,所以对我来说有点难以理解。请调查一下。谢谢
主要活动代码
public class MainActivity extends AppCompatActivity {
//widgets
private Button uploadBtn;
private ImageView imageView;
private ProgressBar progressBar;
private DatabaseReference databasereference = FirebaseDatabase.getInstance().getReference("Image");
private StorageReference storagereference = FirebaseStorage.getInstance().getReference();
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uploadBtn = findViewById(R.id.fabUploadImage);
progressBar = findViewById(R.id.progressBar);
imageView = findViewById(R.id.no_image);
progressBar.setVisibility(View.INVISIBLE);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent , 2);
}
});
uploadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (imageUri != null){
uploadToFirebase(imageUri);
}else{
Toast.makeText(MainActivity.this, "Please Select Image", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode ==2 && resultCode == RESULT_OK && data != null){
imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
private void uploadToFirebase(Uri uri){
final StorageReference fileRef = storagereference.child(System.currentTimeMillis() + "." + getFileExtension(uri));
fileRef.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Model model = new Model(uri.toString());
String modelId = databasereference.push().getKey();
databasereference.child(modelId).setValue(model);
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this, "Uploaded Successfully", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.ic_iamge);
}
});
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
progressBar.setVisibility(View.VISIBLE);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this, "Uploading Failed !!", Toast.LENGTH_SHORT).show();
}
});
}
private String getFileExtension(Uri mUri){
ContentResolver cr = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(cr.getType(mUri));
}
}
我的模型类
public class Model {
private String imageUrl;
public Model()
{
}
public Model(String imageUrl)
{
this.imageUrl = imageUrl;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
1条答案
按热度按时间lmvvr0a81#
正如您在调试代码后发现的那样,您应该设置权限以允许将对象上载到附加到项目的云存储桶。
您应该阅读documentation并根据您的访问权限策略调整安全规则。
请注意,在测试环境中,为了检查你的android代码是否正确(例如,它是否正确上传文件),临时允许每个人都有写权限,这可能会很有趣,为此,你应该使用以下规则。
同样,请勿在生产环境中使用此类规则:他们允许每个人写到你的桶!除非你的应用程序是允许每个未经身份验证的用户上传文件...