当我从图库中选择图像并将其设置为ImageView时,它正在工作。但是当我单击按钮以上传此图像时,进度对话框不会关闭,图像也不会上传到存储中。
显示权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
依赖项Build.gradle(Module:app)
依赖性{
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.firebaseui:firebase-ui-database:3.1.0'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'
impleme;ntation 'com.google.firebase:firebase-auth:16.0.2'
testImplementation 'junit:junit:4.12'}
依赖项Build.gradle(项目)
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:4.0.0'
}
Firebase存储规则的图像
Add_post.java
公共类Add_Post扩展了AppCompatActivity {
private EditText edt_title,edt_descripion;
private ImageView img;
private final int GALLERY_INTENT = 1;
private StorageReference reference;
private Uri uri;
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__post);
reference = FirebaseStorage.getInstance().getReference();
edt_descripion = (EditText) findViewById(R.id.edt_despost);
edt_title = (EditText) findViewById(R.id.edt_titlepost);
img = (ImageView) findViewById(R.id.img_post);
pd = new ProgressDialog(this);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,GALLERY_INTENT);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.addposts_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_submit){
progress_submit();
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
img.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void progress_submit() {
String title = edt_title.getText().toString();
String description = edt_descripion.getText().toString();
pd.setMessage("Uploading...");
pd.setCancelable(false);
pd.show();
if(TextUtils.isEmpty(title) && TextUtils.isEmpty(description) && uri!=null) {
StorageReference filepath = reference.child("images/"+ UUID.randomUUID().toString());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Add_Post.this, "Uploaded", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
}
}
}
答案 0 :(得分:0)
要将图像上传到Firebase存储,请将android服务添加到manifest.xml 下面是添加的示例android服务:-
<service
android:name=".service.ImageUploadService"
android:exported="false" />
演示和源代码由Firebase在此处提供: https://github.com/firebase/quickstart-android/tree/master/storage
答案 1 :(得分:0)
这是一个逻辑错误,因为您仅在检查标题和描述为空时才上传图像。因此,请按以下步骤更改您的代码-
if(!TextUtils.isEmpty(title) && !TextUtils.isEmpty(description) && uri!=null) {
StorageReference filepath = reference.child("images/"+ UUID.randomUUID().toString());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Add_Post.this, "Uploaded", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
}