Reputation: 187
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);
}
}
Upvotes: 1
Views: 4144
Reputation: 297
You can use my code to convert image to Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
And pass bitmap to another activity Link
Note : careful with large bitmap it can cause errors
Solution : Save image into SDCard and in next activity set this image into ImageView.
...
Upvotes: 0
Reputation: 3195
code to get bitmap from imageView
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
Bitmap implements Parcelable, so you could always pass it in the intent:
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("key", bitmap);
getting the bitmap in another activity
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("key");
Upvotes: 1
Reputation: 763
try this
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap",bitmap);
intent.putExtras(bundle);
Upvotes: 0