我正在我的Hashmap中创建一个元素,该元素包含从手机存储上传到Firebase存储的照片的照片路径。如果我上传了多张照片,我的Hashmap中应该有一个元素(图像1,图像2,图像3,.....),它根据上传的照片数量保存照片路径。
每次我运行程序时,都会上传Hashmap“Name,Email”的其他元素,但照片路径(“image”+ i)不会上传。
我希望最终结果在数据库中看起来像这样: the end result in Firebase Database
我的作品有没有错误?无论如何,这有什么作用?
这是我的代码:
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();
}
});
}
}
}
}
这是我的gradle代码:
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:11.8.0'
implementation 'com.google.firebase:firebase-storage:11.8.0'
implementation 'com.google.firebase:firebase-auth:11.8.0'
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'
答案 0 :(得分:0)
尝试使用最新版本的Google服务,例如(项目级别):
classpath 'com.google.gms:google-services:4.0.1'
并用以下内容替换现有代码:
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'
答案 1 :(得分:0)
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();
}
});
}
}
}
}
}
答案 2 :(得分:0)
这里的问题是,当您将图像存储在存储中时,此过程会异步发生。因此,在将图像存储到存储中并在hashmap中添加uri之前,正在调用firebase数据库上的方法:
所以你必须确保在firebase上调用方法之前你已经完成了hashmap过程:
{{1}}