Java 正在将Hashmap变量上载到Firebase数据库
我正在Hashmap中创建一个元素,该元素保存从手机存储器上传到Firebase存储器的照片路径。如果我上传了多张照片,我的Hashmap中应该有一个元素(图1、图2、图3……)根据上传的照片数量保存照片路径 每次我运行这个程序时,Hashmap的其他元素“Name,Email”都会被上传,但是照片路径(“image”+I)不会上传 我希望最终结果在数据库中如下所示: 我的工作中有什么缺陷吗?不管怎样,有什么办法可以让这一切顺利进行吗 这是我的代码:Java 正在将Hashmap变量上载到Firebase数据库,java,android,firebase,firebase-realtime-database,firebase-storage,Java,Android,Firebase,Firebase Realtime Database,Firebase Storage,我正在Hashmap中创建一个元素,该元素保存从手机存储器上传到Firebase存储器的照片路径。如果我上传了多张照片,我的Hashmap中应该有一个元素(图1、图2、图3……)根据上传的照片数量保存照片路径 每次我运行这个程序时,Hashmap的其他元素“Name,Email”都会被上传,但是照片路径(“image”+I)不会上传 我希望最终结果在数据库中如下所示: 我的工作中有什么缺陷吗?不管怎样,有什么办法可以让这一切顺利进行吗 这是我的代码: import android.conten
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
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 java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private Button mSelectimage;
private StorageReference mStorage;
private DatabaseReference mDatabase;
private Uri image;
private String photoPath;
final static int GALLERY_INTENT = 2;
int i;
int j;
int totalItelmsSelected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSelectimage = (Button) findViewById(R.id.button2);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference();
mSelectimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, GALLERY_INTENT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final HashMap <Object,Object> datamap = new HashMap<Object, Object>();
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
if(data.getClipData() != null){
totalItelmsSelected = data.getClipData().getItemCount();
for(i=0; i < totalItelmsSelected; i++){
image = data.getClipData().getItemAt(i).getUri();
final StorageReference filepath = mStorage.child("photos").child(image.getLastPathSegment()+".jpg");
filepath.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
photoPath = filepath.getPath();
datamap.put("image "+i , photoPath);
Toast.makeText(MainActivity.this, i+" Photos has been uploaded.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Problem in Uploading "+i+" Photos.", Toast.LENGTH_SHORT).show();
}
});
}
datamap.put("Name", "Kamil");
datamap.put("Email", "Kamil@gmail.com");
mDatabase.child("users").push().setValue(datamap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Uploading to the database is done", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Problem in registuring the information", Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
尝试使用最新版本的谷歌服务,如(项目级): 并将现有代码替换为以下代码:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.mohammed.storemultipleimages"
minSdkVersion 26
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
public类MainActivity扩展了AppCompatActivity{
私人按钮mSelectimage;
私有存储器;参考存储器;
私有数据库参考数据库;
私有Uri图像;
专用光路;
最终静态int画廊_意向=2;
int i;
int j;
选择整数总和;
字符串push_id;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSelectimage=(按钮)findViewById(R.id.button2);
mStorage=FirebaseStorage.getInstance().getReference();
mDatabase=FirebaseDatabase.getInstance().getReference();
push_id=mDatabase.child(“用户”).push().getKey();
mSelectimage.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意向意向=新意向(意向、行动和选择);
intent.setType(“image/*”);
intent.putExtra(intent.EXTRA_允许_倍数,true);
startActivityForResult(意向、画廊意向);
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
final HashMap datamap=新HashMap();
if(requestCode==GALLERY\u INTENT&&resultCode==RESULT\u OK){
if(data.getClipData()!=null){
totalItelmsSelected=data.getClipData().getItemCount();
对于(i=0;i
}这里的问题是,当您将图像放入存储器时,此过程是异步进行的。所以,在将图像放入存储器并在hashmap中添加uri之前,将调用firebase数据库上的方法: 因此,您必须确保在firebase上调用方法之前已完成hashmap过程:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final HashMap <Object,Object> datamap = new HashMap<Object, Object>();
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
if(data.getClipData() != null){
totalItelmsSelected = data.getClipData().getItemCount();
for(i=0; i < totalItelmsSelected; i++){
image = data.getClipData().getItemAt(i).getUri();
final StorageReference filepath = mStorage.child("photos").child(image.getLastPathSegment()+".jpg");
filepath.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
photoPath = filepath.getPath();
datamap.put("image "+i , photoPath);
datamap.put("Name", "Kamil");
datamap.put("Email", "Kamil@gmail.com");
mDatabase.child("users").push().setValue(datamap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Uploading to the database is done", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Problem in registuring the information", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(MainActivity.this, i+" Photos has been uploaded.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Problem in Uploading "+i+" Photos.", Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
@覆盖
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
final HashMap datamap=新HashMap();
if(requestCode==GALLERY\u INTENT&&resultCode==RESULT\u OK){
if(data.getClipData()!=null){
totalItelmsSelected=data.getClipData().getItemCount();
对于(i=0;iapply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.mohammed.storemultipleimages"
minSdkVersion 26
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
public class MainActivity extends AppCompatActivity {
private Button mSelectimage;
private StorageReference mStorage;
private DatabaseReference mDatabase;
private Uri image;
private String photoPath;
final static int GALLERY_INTENT = 2;
int i;
int j;
int totalItelmsSelected;
String push_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSelectimage = (Button) findViewById(R.id.button2);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference();
push_id = mDatabase.child("users").push().getKey();
mSelectimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, GALLERY_INTENT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final HashMap <Object,Object> datamap = new HashMap<Object, Object>();
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
if(data.getClipData() != null){
totalItelmsSelected = data.getClipData().getItemCount();
for(i=0; i < totalItelmsSelected; i++){
image = data.getClipData().getItemAt(i).getUri();
final StorageReference filepath = mStorage.child("photos").child(image.getLastPathSegment()+".jpg");
filepath.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
photoPath = filepath.getPath();
datamap.put("image "+i , photoPath);
Toast.makeText(MainActivity.this, i+" Photos has been uploaded.", Toast.LENGTH_SHORT).show();
datamap.put("Name", "Kamil");
datamap.put("Email", "Kamil@gmail.com");
mDatabase.child("users").child(push_id).setValue(datamap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Uploading to the database is done", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Problem in registuring the information", Toast.LENGTH_SHORT).show();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Problem in Uploading "+i+" Photos.", Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final HashMap <Object,Object> datamap = new HashMap<Object, Object>();
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
if(data.getClipData() != null){
totalItelmsSelected = data.getClipData().getItemCount();
for(i=0; i < totalItelmsSelected; i++){
image = data.getClipData().getItemAt(i).getUri();
final StorageReference filepath = mStorage.child("photos").child(image.getLastPathSegment()+".jpg");
filepath.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
photoPath = filepath.getPath();
datamap.put("image "+i , photoPath);
datamap.put("Name", "Kamil");
datamap.put("Email", "Kamil@gmail.com");
mDatabase.child("users").push().setValue(datamap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Uploading to the database is done", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Problem in registuring the information", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(MainActivity.this, i+" Photos has been uploaded.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Problem in Uploading "+i+" Photos.", Toast.LENGTH_SHORT).show();
}
});
}
}
}
}