When i choose an image from gallery and set it to ImageView it is working . but when i click on the button to uploading this image progressdialog doesn't dismiss and image will not be uploaded to storage.
Manifiest permission
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
dependencies Build.gradle(Module:app)
dependencies {
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'}
dependencies Build.gradle(project)
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:4.0.0'
}
image of Firebase storage rules
Add_post.java
public class Add_Post extends 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();
}
});
}
}
}
To upload the image to Firebase storage, add an android service into your manifest.xml Below was a sample android service added:-
<service
android:name=".service.ImageUploadService"
android:exported="false" />
The demo and source code were provided by Firebase here: https://github.com/firebase/quickstart-android/tree/master/storage
It's a logical bug, as you are checking when title & description is empty only then upload the image. So change your code as done below-
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();
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.