Java 在Firebase-Android Studio上上载多个图像
嗨,伙计们,我正试图在firebase上上传3张带有标题的图片,但它只成功上传了第一张,然后上传了图片2和3的字段。它在数据库上复制了图片1的下载url。有人能帮我解决这个问题,让每个图像都有自己的下载url吗 这是我的XMLJava 在Firebase-Android Studio上上载多个图像,java,android,firebase,firebase-realtime-database,firebase-storage,Java,Android,Firebase,Firebase Realtime Database,Firebase Storage,嗨,伙计们,我正试图在firebase上上传3张带有标题的图片,但它只成功上传了第一张,然后上传了图片2和3的字段。它在数据库上复制了图片1的下载url。有人能帮我解决这个问题,让每个图像都有自己的下载url吗 这是我的XML <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="h
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ke.co.tapeli.tapeli.UploadActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Image 1" />
<ImageButton
android:id="@+id/imageSelect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="@android:color/white"
android:scaleType="centerCrop"
app:srcCompat="@drawable/add_btn" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Image 2" />
<ImageButton
android:id="@+id/imageSelect1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="@android:color/white"
android:scaleType="centerCrop"
app:srcCompat="@drawable/add_btn" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Image 3" />
<ImageButton
android:id="@+id/imageSelect2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="@android:color/white"
android:scaleType="centerCrop"
app:srcCompat="@drawable/add_btn" />
<EditText
android:id="@+id/titleField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_outline"
android:drawableLeft="@drawable/ic_con_name"
android:ems="10"
android:hint="Conman's Name"
android:inputType="textPersonName"
android:padding="7dp"
android:singleLine="true"
android:textSize="14sp" />
<EditText
android:id="@+id/descField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@drawable/input_outline"
android:drawableLeft="@drawable/ic_con_desc"
android:ems="10"
android:hint="Crime Description"
android:inputType="textMultiLine"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/phoneField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_outline"
android:drawableLeft="@drawable/ic_con_phone"
android:ems="10"
android:hint="Conman's Number"
android:inputType="textMultiLine"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/passField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_outline"
android:drawableLeft="@drawable/ic_con_pass"
android:ems="10"
android:hint="Conman's ID Number/ Passport "
android:inputType="textMultiLine"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/otherField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_outline"
android:drawableLeft="@drawable/ic_con_info"
android:ems="10"
android:hint="Any other Information"
android:inputType="textMultiLine"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/stepsField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_outline"
android:drawableLeft="@drawable/ic_con_steps"
android:ems="10"
android:hint="Steps Taken (Eg. Went to court)"
android:inputType="textMultiLine"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="@+id/policeField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_outline"
android:drawableLeft="@drawable/ic_con_police"
android:ems="10"
android:hint="Police Station Reported To"
android:inputType="textMultiLine"
android:padding="7dp"
android:textSize="14sp" />
<Button
android:id="@+id/submitBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:background="@color/colorPrimary"
android:text="Report a Tapeli"
android:textColor="@android:color/white" />
</LinearLayout>
</ScrollView>
</FrameLayout>
这是我的Java代码
package ke.co.tapeli.tapeli;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import ke.co.tapeli.tapeli.Auths.Login;
public class UploadActivity extends AppCompatActivity {
private ImageButton mSelectImage;
private ImageButton mSelectImage1;
private ImageButton mSelectImage2;
private EditText mPostTitle;
private EditText mPostDesc;
private EditText mPostNumber;
private EditText mPostPass;
private EditText mPostInfo;
private EditText mPostSteps;
private EditText mPostPolice;
private Button mSubmitBtn;
private Uri mImageUri = null;
private Uri mImageUri1 = null;
private Uri mImageUri2 = null;
private StorageReference mStorage;
private DatabaseReference mDatabase;
private ProgressDialog mProgress;
private static final int GALLERY_REQUEST = 1;
private static final int GALLERY_REQUEST2 = 2;
private static final int GALLERY_REQUEST3 = 3;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
mAuth = FirebaseAuth.getInstance();
mAuthListner = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() == null){
Intent loginIntent = new Intent(UploadActivity.this, Login.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}
}
};
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Matapeli");
mSelectImage = (ImageButton) findViewById(R.id.imageSelect);
mSelectImage1 = (ImageButton) findViewById(R.id.imageSelect1);
mSelectImage2 = (ImageButton) findViewById(R.id.imageSelect2);
mPostTitle = (EditText) findViewById(R.id.titleField);
mPostDesc = (EditText) findViewById(R.id.descField);
mPostNumber = (EditText) findViewById(R.id.phoneField);
mPostPass = (EditText) findViewById(R.id.passField);
mPostInfo = (EditText) findViewById(R.id.otherField);
mPostSteps = (EditText) findViewById(R.id.stepsField);
mPostPolice = (EditText) findViewById(R.id.policeField);
mProgress = new ProgressDialog(this);
mSubmitBtn = (Button) findViewById(R.id.submitBtn);
mSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
});
mSelectImage1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(galleryIntent, GALLERY_REQUEST2);
}
});
mSelectImage2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(galleryIntent, GALLERY_REQUEST3);
}
});
mSubmitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startPosting();
}
});
}
private void startPosting() {
mProgress.setMessage("Adding a Tapeli...");
final String title_val = mPostTitle.getText().toString().trim();
final String desc_val = mPostDesc.getText().toString().trim();
final String namba_val = mPostNumber.getText().toString().trim();
final String pass_val = mPostPass.getText().toString().trim();
final String info_val = mPostInfo.getText().toString().trim();
final String steps_val = mPostSteps.getText().toString().trim();
final String police_val = mPostPolice.getText().toString().trim();
if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri !=null){
mProgress.show();
StorageReference filepath = mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());
StorageReference filepath1 = mStorage.child("Blog_Images").child(mImageUri1.getLastPathSegment());
StorageReference filepath2 = mStorage.child("Blog_Images").child(mImageUri2.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadurl = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(desc_val);
newPost.child("number").setValue(namba_val);
newPost.child("passport").setValue(pass_val);
newPost.child("info").setValue(info_val);
newPost.child("steps").setValue(steps_val);
newPost.child("police").setValue(police_val);
newPost.child("image").setValue(downloadurl.toString());
newPost.child("image1").setValue(downloadurl.toString());
newPost.child("image2").setValue(downloadurl.toString());
mProgress.dismiss();
finish();
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
mImageUri = data.getData();
mSelectImage.setImageURI(mImageUri);
}
if(requestCode == GALLERY_REQUEST2 && resultCode == RESULT_OK){
mImageUri1 = data.getData();
mSelectImage1.setImageURI(mImageUri1);
}
if(requestCode == GALLERY_REQUEST3 && resultCode == RESULT_OK){
mImageUri2 = data.getData();
mSelectImage2.setImageURI(mImageUri2);
}
}
}
包ke.co.tapeli.tapeli;
导入android.app.FragmentTransaction;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.net.Uri;
导入android.support.annotation.NonNull;
导入android.support.v4.app.FragmentManager;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.text.TextUtils;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.Toast;
导入com.google.android.gms.tasks.OnSuccessListener;
导入com.google.firebase.auth.FirebaseAuth;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
导入com.google.firebase.storage.firebase存储;
导入com.google.firebase.storage.StorageReference;
导入com.google.firebase.storage.UploadTask;
导入ke.co.tapeli.tapeli.Auths.Login;
公共类UploadActivity扩展了AppCompatActivity{
私有图像按钮mSelectImage;
私有图像按钮mSelectImage1;
私有图像按钮mSelectImage2;
私人编辑标题;
私有编辑文本mPostDesc;
私有编辑文本mPostNumber;
私有编辑类;
私有编辑文本mPostInfo;
私有EditText-mPostSteps;
私有编辑策略;
专用按钮mSubmitBtn;
私有Uri mimageri=null;
私有Uri mimageri1=null;
私有Uri mimageri2=null;
私有存储器;参考存储器;
私有数据库参考数据库;
私人进程;
私有静态最终int库_请求=1;
私有静态最终整数库_REQUEST2=2;
私有静态最终整数库_REQUEST3=3;
私人消防队;
私有FirebaseAuth.AuthStateListener mAuthListner;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_上传);
mAuth=FirebaseAuth.getInstance();
mAuthListner=new FirebaseAuth.AuthStateListener(){
@凌驾
AuthStateChanged上的公共void(@NonNull FirebaseAuth FirebaseAuth){
如果(firebaseAuth.getCurrentUser()==null){
Intent loginIntent=newintent(UploadActivity.this,Login.class);
登录。添加标志(意图。标志\活动\清除\顶部);
startActivity(登录);
}
}
};
mStorage=FirebaseStorage.getInstance().getReference();
mDatabase=FirebaseDatabase.getInstance().getReference().child(“Matapeli”);
mSelectImage=(ImageButton)findViewById(R.id.imageSelect);
mSelectImage1=(ImageButton)findViewById(R.id.imageSelect1);
mSelectImage2=(ImageButton)findViewById(R.id.imageSelect2);
mPostTitle=(EditText)findViewById(R.id.titleField);
mPostDesc=(EditText)findViewById(R.id.descField);
mPostNumber=(EditText)findViewById(R.id.phoneField);
mPostPass=(EditText)findViewById(R.id.passField);
mPostInfo=(EditText)findViewById(R.id.otherField);
mPostSteps=(EditText)findViewById(R.id.stepsField);
mpospolice=(EditText)findViewById(R.id.policyField);
mpprogress=新建进度对话框(此对话框);
mSubmitBtn=(按钮)findViewById(R.id.submitBtn);
mSelectImage.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent gallerycontent=新的意图(Intent.ACTION\u GET\u CONTENT);
GalleryContent.setType(“image/*”);
GalleryContent.putExtra(Intent.EXTRA\u允许\u倍数,true);
startActivityForResult(画廊内容、画廊申请);
}
});
mSelectImage1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent gallerycontent=新的意图(Intent.ACTION\u GET\u CONTENT);
GalleryContent.setType(“image/*”);
GalleryContent.putExtra(Intent.EXTRA\u允许\u倍数,true);
startActivityForResult(画廊内容,画廊2);
}
});
mSelectImage2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent gallerycontent=新的意图(Intent.ACTION\u GET\u CONTENT);
GalleryContent.setType(“image/*”);
GalleryContent.putExtra(Intent.EXTRA\u允许\u倍数,true);
startActivityForResult(画廊内容,画廊3);
}
});
mSubmitBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
startPosting();
}
});
}
私有无效起始位置(){
setMessage(“添加磁带…”);
最终字符串title_val=mPostTitle.getText().toString().trim();
最终字符串desc_val=mPostDesc.getText().toString().trim();
最后一个字符串namba_val=mPostNumber.getText().toString().trim();
最终字符串传递_val=mPostPass.getText().toString().trim();
final String info_val=mPostInfo.getText().toString().trim();
StorageReference filepath = mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());
StorageReference filepath1 = mStorage.child("Blog_Images").child(mImageUri1.getLastPathSegment());
StorageReference filepath2 = mStorage.child("Blog_Images").child(mImageUri2.getLastPathSegment());
final DatabaseReference newPost = mDatabase.push();
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadurl = taskSnapshot.getDownloadUrl();
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(desc_val);
newPost.child("number").setValue(namba_val);
newPost.child("passport").setValue(pass_val);
newPost.child("info").setValue(info_val);
newPost.child("steps").setValue(steps_val);
newPost.child("police").setValue(police_val);
newPost.child("image").setValue(downloadurl.toString());
}
});
filepath1.putFile(mImageUri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadurl = taskSnapshot.getDownloadUrl();
newPost.child("image1").setValue(downloadurl.toString());
}
});
filepath2.putFile(mImageUri2).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadurl = taskSnapshot.getDownloadUrl();
newPost.child("image2").setValue(downloadurl.toString());
mProgress.dismiss();
finish();
}
});
filepath1.putFile(mImageUri1).addOnSuccessListener(/* ... */);
filepath2.putFile(mImageUri2).addOnSuccessListener(/* ... */);
filepath3.putFile(mImageUri3).addOnSuccessListener(/* ... */);
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadurl = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(desc_val);
newPost.child("number").setValue(namba_val);
newPost.child("passport").setValue(pass_val);
newPost.child("info").setValue(info_val);
newPost.child("steps").setValue(steps_val);
newPost.child("police").setValue(police_val);
newPost.child("image").setValue(downloadurl.toString());
newPost.child("image1").setValue(downloadurl.toString());
newPost.child("image2").setValue(downloadurl.toString());
mProgress.dismiss();
finish();
}
});
filepath1.putFile(mImageUri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadurl = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(desc_val);
newPost.child("number").setValue(namba_val);
newPost.child("passport").setValue(pass_val);
newPost.child("info").setValue(info_val);
newPost.child("steps").setValue(steps_val);
newPost.child("police").setValue(police_val);
newPost.child("image").setValue(downloadurl.toString());
newPost.child("image1").setValue(downloadurl.toString());
newPost.child("image2").setValue(downloadurl.toString());
mProgress.dismiss();
finish();
}
});
filepath2.putFile(mImageUri2).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadurl = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(desc_val);
newPost.child("number").setValue(namba_val);
newPost.child("passport").setValue(pass_val);
newPost.child("info").setValue(info_val);
newPost.child("steps").setValue(steps_val);
newPost.child("police").setValue(police_val);
newPost.child("image").setValue(downloadurl.toString());
newPost.child("image1").setValue(downloadurl.toString());
newPost.child("image2").setValue(downloadurl.toString());
mProgress.dismiss();
finish();
}
});