我启动了我的应用程序,在newJoke textview中撰写了一篇文章,在newMeme imageview中添加了图片,然后单击了newJokeButton按钮。将该帖子添加到firebase实时数据库中,并将图像存储在firebase存储器中的次数与我单击按钮的次数相同。但是,然后,我在模拟器上按了返回键,然后再次进入发布页面,但是随后在单击发布按钮之后,应用程序继续停止。请帮助
我面临的第二个问题是,如果newJoke为空,我不希望将图像添加到存储中。我该如何实现?
我尝试卸载并重新启动应用程序。我在logcat中搜索错误,但没有找到解决方法。
PostActivity.java
public class PostActivity extends AppCompatActivity {
public static int RESULT_LOAD_IMAGE = 1;
private EditText newJoke;
private Button newJokeBtn;
private ImageButton newMemeBtn;
private Uri uri = null;
private ImageView newMeme;
private static final String TAG = "PostActivity";
DatabaseReference databasePosts;
private StorageReference storage;
private FirebaseUser user;
private StorageReference memesRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.HomeTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
databasePosts = FirebaseDatabase.getInstance().getReference("posts");
storage = FirebaseStorage.getInstance().getReference("memes");
memesRef = FirebaseStorage.getInstance().getReference();
user = FirebaseAuth.getInstance().getCurrentUser();
newJoke = findViewById(R.id.editjoke);
newJokeBtn = findViewById(R.id.postBtn);
newMeme = findViewById(R.id.imagememe);
newMemeBtn = findViewById(R.id.memebutton);
newMemeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getImageFromAlbum();
}
});
newJokeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addPost();
final Animation myAnim = AnimationUtils.loadAnimation(PostActivity.this, R.anim.milkshake);
newJokeBtn.setAnimation(myAnim);
view.startAnimation(myAnim);
newJoke.setText("");
newMeme.setImageDrawable(null);
}
});
}
private void getImageFromAlbum() {
try {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
} catch (Exception exp) {
Log.i("Error", exp.toString());
}
}
public void addPost() {
String joke = newJoke.getText().toString().trim();
String memeuri = uri.toString();
if (!TextUtils.isEmpty(joke)) {
String id = databasePosts.push().getKey();
Post post = new Post(id, joke, memeuri);
databasePosts.child(id).setValue(post);
Toast.makeText(getApplicationContext(), "Haha", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult ( int requestCode, int resultCode, @Nullable final Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
try {
Uri selectedImageUri = data.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImageUri);
Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
newMeme.setImageBitmap(selectedImage);
// Get a reference to store file at chat_photos/<FILENAME>
final StorageReference photoRef = storage.child("memes").child(selectedImageUri
.getLastPathSegment());
// Upload file to Firebase Storage
photoRef.putFile(selectedImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Download file From Firebase Storage
photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri downloadPhotoUrl) {
uri = downloadPhotoUrl;
}
});
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(PostActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme.Launcher">
<activity android:name=".PostActivity"></activity>
<activity android:name=".FeedActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Post.java(Java模型类)
public class Post {
String postId;
String postJoke;
String postMeme;
public Post(){}
public Post(String postId, String postJoke, String postMeme) {
this.postId = postId;
this.postJoke = postJoke;
this.postMeme = postMeme;
}
public String getPostId() {
return postId;
}
public String getPostJoke() {
return postJoke;
}
public String getPostMeme() {
return postMeme;
}
}
请帮助我。