如何解决将多张图片添加到Firebase
我知道这个问题已经问了很多。但实际上没有答案对我有用。 我的问题是图像似乎已保存但在Firebase上,但不幸的是它们的名称相同,因此firebase删除了旧图像,并插入了新图像。我已经做了一切可以更改名称的事情,但找不到办法。
这是我要上传的代码!
private void Fileuploader() throws FileNotFoundException {
String imageid;
imageid = arabic.getText().toString()+"-"+System.currentTimeMillis()+"."+"jpg";
String id = getIntent().getStringExtra("storeid");
member.setImageid(imageid);
reference.child(id).child(arname).setValue(member);
progress.showProgress(profuctadd.this,"Loading...",false);
DatabaseHelper databaseHelper = new DatabaseHelper(profuctadd.this);
Cursor getimage = databaseHelper.GetPath();
while (getimage.moveToNext()){
Bitmap bm = BitmapFactory.decodeFile(getimage.getString(0));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,35,out);
byte[] data = out.toByteArray();
System.out.println("IMAGES UPLOADEDDDD: "+ data);
StorageReference Ref = mStorageRef.child(imageid);
Ref.putBytes(data)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Get a URL to the uploaded content
//Uri downloadUrl = taskSnapshot.getDownloadUrl();
//Toast.makeText(profuctadd.this,"Image uploaded",Toast.LENGTH_LONG).show();
progress.hideProgress();
Intent intent = new Intent(profuctadd.this,showProducts.class);
intent.putExtra("spec",getIntent().getStringExtra("spec"));
intent.putExtra("storeid",getIntent().getStringExtra("storeid"));
startActivity(intent);
DatabaseHelper mDatabaseHelper = new DatabaseHelper(profuctadd.this);
Cursor cursor2 = mDatabaseHelper.DeleteDataOfTableImagesAr();
cursor2.moveToNext();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
// ...
Toast.makeText(profuctadd.this,"Failed",Toast.LENGTH_LONG).show();
}
});
}
}
有什么想法吗?
解决方法
您在所有图像上只计算一次imageid
,因此每个图像将具有相同的名称。您将需要为每个图像计算一个新值,并且可能还会抛出一个计数器,以确保它们都是唯一的:
private void Fileuploader() throws FileNotFoundException {
String id = getIntent().getStringExtra("storeid");
member.setImageid(imageid);
reference.child(id).child(arname).setValue(member);
progress.showProgress(profuctadd.this,"Loading...",false);
DatabaseHelper databaseHelper = new DatabaseHelper(profuctadd.this);
Cursor getimage = databaseHelper.GetPath();
int count = 0;
while (getimage.moveToNext()){
Bitmap bm = BitmapFactory.decodeFile(getimage.getString(0));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,35,out);
byte[] data = out.toByteArray();
System.out.println("IMAGES UPLOADEDDDD: "+ data);
StorageReference Ref = mStorageRef.child(imageid);
String imageid = arabic.getText().toString()+"-"+System.currentTimeMillis()+"_"+(count++)+"."+"jpg";
Ref.putBytes(data)
...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。