[英]Firebase app crash while uploading image
我的應用程序需要將圖像上傳到Firebase存儲並在recyclerView中顯示。 我的前兩張圖片在列表上顯示得很好。 但是,當我嘗試上傳第三張圖片時,應用程序崩潰了,並且也無法將圖片存儲在Firebase上。 每個圖像大小從500KB到700KB不等。 我在清單中也使用了android:largeHeap =“ true”。 該怎么辦?
//這是代碼:
public class PostActivity extends Activity {
private EditText postTitle;
private EditText postDescription;
private Button submitButton;
private Uri imageUri=null;
private StorageReference storage;
private ProgressDialog progressDialogue;
private DatabaseReference database;
public String titleVal;
public String descriptionVal;
private ImageView mImageView;
private static final int GALLERY_REQUEST=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
storage= FirebaseStorage.getInstance().getReference();
database= FirebaseDatabase.getInstance().getReference().child("Posts");
mImageView=(ImageView) findViewById(R.id.imageView);
postTitle=(EditText) findViewById(R.id.titleField);
postDescription=(EditText)findViewById(R.id.descriptionField);
submitButton=(Button) findViewById(R.id.submitPost);
progressDialogue=new ProgressDialog(this);
imageUri = Uri.parse(getIntent().getStringExtra("imageUri"));
ImageView img = (ImageView) findViewById(R.id.imageView);
img.setImageURI(imageUri);
//提交后會發生什么。
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startPosting();
}
});
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("titleVal", titleVal);
outState.putString("descriptionVal",descriptionVal );
}
private void startPosting() {
titleVal=postTitle.getText().toString().trim();
descriptionVal=postDescription.getText().toString().trim();
if(!TextUtils.isEmpty(titleVal) && !TextUtils.isEmpty(descriptionVal) && imageUri!=null){
progressDialogue.setMessage("আপলোড হচ্ছে......");
progressDialogue.show();
StorageReference filePath=storage.child("post_images").child(imageUri.getLastPathSegment());
filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl=taskSnapshot.getDownloadUrl();
Picasso.with(PostActivity.this).load(downloadUrl).into(mImageView);
DatabaseReference newPost=database.push();
newPost.child("title").setValue(titleVal);
newPost.child("description").setValue(descriptionVal);
newPost.child("image").setValue(downloadUrl.toString());
progressDialogue.dismiss();
Toast.makeText(getApplicationContext(), "Successfully Uploaded", Toast.LENGTH_LONG).show();
startActivity(new Intent(PostActivity.this,HomeActivity.class));
}
});
}else{
Toast.makeText(getApplicationContext(), "Please provide a Title", Toast.LENGTH_LONG).show();
}
}
}
嘗試下面的代碼,我正在使用此代碼將圖像上傳到Firebase存儲,如果您發現任何問題或有任何疑問,請告訴我
1-在您的Java文件中,放入以下代碼
private SimpleDraweeView imgProfile;
private FirebaseStorage storage;
private Uri file, resultUri;
private StorageMetadata metadata;
private UploadTask uploadTask;
private StorageReference storageRef;
private final String MY_BUCKET_PATH = "YOUR BUCKET PATH OF YOUR STORAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
storage = FirebaseStorage.getInstance();
storageRef = storage.getReferenceFromUrl(MY_BUCKET_PATH);
}
2-在onActivityResult中獲取圖像的Uri后,將圖像上傳到Firebase存儲中,還請確保獲取正確的圖像Uri
private void UploadImage() {
file = Uri.fromFile(new File(resultUri.getPath()));
// Create the file metadata
metadata = new StorageMetadata.Builder().setContentType("image/jpg").build();
// Upload file and metadata to the path 'images/mountains.jpg'
uploadTask = storageRef.child("images/" + file.getLastPathSegment()).putFile(file, metadata);
// Listen for state changes, errors, and completion of the upload.
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
showUploadProgress(progress);
System.out.println("Upload is " + progress + "% done");
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.e("Uploading image==", "onSuccess");
// Handle successful uploads on complete
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
imgProfile.setImageURI(Uri.parse(downloadUrl.toString())); // Here image will be reflected after uploading
mDialog.dismiss();
Log.e("DownloadUrl getPath==>>", taskSnapshot.getDownloadUrl().getPath());
Log.e("Metadata getPath==>>", taskSnapshot.getMetadata().getPath()); // this line will give you path to download Image from Firebase storage omitting child storage reference
Log.e("Metadata dwndUrl getpath", taskSnapshot.getMetadata().getDownloadUrl().getPath());
Log.e("storageRef path==>>", taskSnapshot.getMetadata().getReference().getPath());
mSessionManager.storeStringValues("profile_photo", downloadUrl.toString());
mSessionManager.storeStringValues("profile_photo_path", taskSnapshot.getMetadata().getPath());
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.