I am making a meme generator app for my android course. I've gotten an API to generate 100 popular memes, when one is clicked it takes you to the EditMemeActivity, where you can type in the upper and lower text. Then, there is a Create Meme button that will take you to the MemeActivity where you will eventually be able to save/share with friends. Currently, when the create meme button is clicked, the meme picture is converted to a Bitmap, and that displays fine on the next page. I want to be able to save the upper and lower text entered by the user on the image as a Bitmap. Because some meme images vary in size, I have a black background around them set to about 400X300 pixels, so I would love to be able to capture that entire imageview AND set put the inputted text in. Here's my code from the two activities:
public class EditMemeActivity extends AppCompatActivity {
@Bind(R.id.editMemeImage) ImageView mEditMemeImage;
@Bind(R.id.editUpperText) EditText mEditUpperText;
@Bind(R.id.editLowerText) EditText mEditLowerText;
@Bind(R.id.saveMeme) Button mSaveMeme;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_meme);
ButterKnife.bind(this);
Intent intent = getIntent();
String image = intent.getStringExtra("image");
final String upper = mEditUpperText.getText().toString();
final String lower = mEditLowerText.getText().toString();
final Bitmap memeBitmap = getBitmapFromURL(image);
Picasso.with(EditMemeActivity.this).load(image).into(mEditMemeImage);
mSaveMeme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(EditMemeActivity.this, MemeActivity.class);
intent.putExtra("bitmap", memeBitmap);
intent.putExtra("upper", upper);
intent.putExtra("lower", lower);
startActivity(intent);
}
});
}
public static Bitmap getBitmapFromURL(String image) {
try {
URL url = new URL(image);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
public class MemeActivity extends AppCompatActivity {
@Bind(R.id.memeImageView) ImageView mMemeImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meme);
ButterKnife.bind(this);
Intent intent = getIntent();
String upperText = intent.getStringExtra("upper");
String lowerText = intent.getStringExtra("lower");
byte[] byteArray = getIntent().getByteArrayExtra("bitmap");
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
mMemeImageView.setImageBitmap(bitmap);
}
}