1
我的應用需要將圖片上傳到firebase存儲並將其顯示在recyclerView中。我的前兩個圖像顯示在列表中。但是,當我嘗試上傳第三張圖片時,應用程序崩潰,並且無法將圖像存儲在firebase上。每個圖像大小從500KB到700KB不等。我也在我的清單中使用了android:largeHeap =「true」。該怎麼辦?上傳圖片時Firebase應用崩潰
//下面的代碼:
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);
//職位是SUBMITED後會發生什麼。
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();
}
}
}
你的錯誤日誌說什麼? – Piyush
如何將圖像存儲在Firebase中?例如:Base64字符串或內置文件上傳方法 – R2R
我正在使用字符串。我也應該在這裏粘貼代碼。我不是嗎?現在可以粘貼嗎? –