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
我正在使用字符串。我也应该在这里粘贴代码。我不是吗?现在可以粘贴吗? –