I'm trying to draw a Bitmap on a ImageView, but it is not showing... I created the Bitmap and stored on a Intent Extra, but on the new activity I'm not able to draw it on the ImageView.
Here is my code, calling the new activity:
public void onClickShare(View v) {
Intent myIntent = new Intent(this, Share.class);
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
myIntent.putExtra("createdImg", b);
startActivity(myIntent);
}
And on the new activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
backgroundImg = (ImageView)findViewById(R.id.imageView1);
Intent myIntent = getIntent();
Bitmap bitmap = (Bitmap) myIntent.getParcelableExtra("createdImg");
backgroundImg.setImageBitmap(bitmap);
}
What am I missing? Thanks!
Try to send it as ByteArray and decode it in the receiving activity.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Share.class);
in1.putExtra("image",byteArray);
in the second activity
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
You can also try like this
Bundle bundle = new Bundle();
bundle.putParcelable("createdImg", b);
intent.putExtras(bundle);
and in next activity
Bundle bundle = this.getIntent().getExtras();
Bitmap bmap = bundle.getParcelable("createdImg");
as given in this tutorial http://android-er.blogspot.com/2011/10/pass-bitmap-between-activities.html
I took it a little bit from every answer, plus other researches, and I came up with the code below that worked nicely:
Calling the new activity:
public void onClickShare(View v) {
Intent myIntent = new Intent(this, Share.class);
// create bitmap screen capture
Bitmap bitmap;
View v1 = v.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
// create a new one with the area I want
Bitmap bmpLast = Bitmap.createBitmap(bitmap, 25, 185, 430, 520);
v1.setDrawingCacheEnabled(false);
//create an empty bitmap with the size I need
Bitmap bmOverlay = Bitmap.createBitmap(480, 760, Bitmap.Config.ARGB_8888);
//draw the content on the bitmap
Canvas c = new Canvas(bmOverlay);
c.drawBitmap(bmpLast, 0, 0, null);
//compress the bitmap to send to other activity
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmpLast.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
//store compressed bitmap
myIntent.putExtra("createdImg", byteArray);
startActivity(myIntent);
}
And on the new activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
//decompress the bitmap
byte[] byteArray = getIntent().getByteArrayExtra("createdImg");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
//set the imageview with the bitmap
backgroundImg = (ImageView)findViewById(R.id.imageView1);
backgroundImg.setImageDrawable(new BitmapDrawable(getResources(), bmp));
}
Thanks everyone! =)
Related
I am recently trying to save image and text with sharedpreferences, but saving image does not go well.
this is the main activity code :
imageView = (ImageView) findViewById(R.id.imageview);
Button uploading = (Button) findViewById(R.id.upload_upload);
uploading.setOnClickListener(new View.OnClickListener() { // UPLOAD BUTTON
#Override
public void onClick(View view) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
float scale = (float) (1024/(float)bitmap.getWidth());
int image_w = (int) (bitmap.getWidth() * scale);
int image_h = (int) (bitmap.getHeight() * scale);
Bitmap resize = Bitmap.createScaledBitmap(bitmap, image_w, image_h, true);
resize.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(upload.this,MainActivity.class);
intent.putExtra("picture", byteArray);
intent.putExtra("integer", 300);
intent.putExtra("double", 3.141592);
intent.putExtra("image", byteArray);
and this is the subactivity which sets image.
imageView = (ImageView) findViewById(R.id.main_image);
byte[] byteArray = getIntent().getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imageView.setImageBitmap(bitmap);
you need to convert image into Base64 string representation by using below code:
String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("image_data",encodedImage);
edit.commit();
and then, when retrieving, convert it back into bitmap:
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
String previouslyEncodedImage = shre.getString("image_data", "");
if( !previouslyEncodedImage.equalsIgnoreCase("") ){
byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
imageConvertResult.setImageBitmap(bitmap);
}
I uploaded an Image from my Internal Storage and want now to pass this
selected Image to a new Activity. I do not pass an Image from the
drawable. I pass an Image which I selected from my Internal Storage. I
tried to pass this Image via the R.id
ImageView imageView_selectedImage;
imageView_selectedImage =(ImageView)findViewById(R.id.imageView_selectedImage);
Button button_goToNextActivity = (Button) findViewById(R.id.button_goToNextActivity);
button_goToNextActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, nextActivity.class);
intent.putExtra("resId", R.id.selectedImage);
startActivity(intent);
}
}
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
Then pass the byte array to the next activity.
Intent intent = new Intent(this, nextActivity.class);
intent.putExtra("image", imageEncoded);
startActivity(intent);
Get the byte from the intent second activity.
Bitmap bmp;
byte[] byteArray = getIntent().getByteArrayExtra("image");
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Set the bitmap to the imageview of the second activty
imageview.setImageBitmap(bmp);
Don't pass imageview to next activity.
You can pass uri of Image which is selected from gallery.\
Get Uri for selected image in onActivityResult method like below code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_GALLERY) {
if (data != null) {
imageSelectedFromGallery(data);
pictureUri=data.getData();
imageView_selectedImage.setImageURI(pictureUri);
}
}
}
Note**: make pictureUri instance variable instead of local variable.
Pass Image Uri to next Activity like below code:
Intent intent=new Intent(Main2Activity.this,NextActivity.class);
intent.putExtra("pictureUri",pictureUri);
startActivity(intent);
Get Image Uri in Next Activity and set into imageView :
ImageView imageview=findViewById(R.id.imageview);
Bundle bundle=getIntent().getExtras();
if(bundle!=null) {
String pictureUri = bundle.getString("pictureUri");
imageview.setImageURI( Uri.parse(pictureUri));
}
I hope its work for you.
I want to send and receive image between the two activity using
Intent intent = new Intent(this, second.class) in Android.
first you have to convert the image in byte array
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
send byte array through intent
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
Get Byte Array from Bundle and Convert into Bitmap Image:
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
I have try convering with bitmap but the problem is that the image look bed here is what i did :
submitPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customImage.setDrawingCacheEnabled(true);
customImage.buildDrawingCache();
//Bitmap bit =Bitmap.createBitmap(customImage.getWidth(), customImage.getHeight(),Bitmap.Config.ARGB_8888);
//Canvas canvas = new Canvas(bit);
//personPhoto.draw(canvas);
Bitmap bitmap=Bitmap.createBitmap( customImage.getDrawingCache());
customImage.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Intent intent=new Intent(OnPhotoTakedActivity.this,OnPhotoSubmit.class);
intent.putExtra("bitmap",scaleDownBitmap(bitmap,120,getApplicationContext()));
startActivity(intent);
}
});
and in the next activity :
Intent intent = getIntent();
Bitmap bitmap=intent.getExtras().getParcelable("bitmap");
imageView.setImageBitmap(bitmap);
I tryed all the codes on this and other webs but still the image look bad
thanks any way.
I got this image from the server. The image is not in the project resource folder. The text is working correctly, but the image is not showing in another activity.
MyAdapter objAdapter1;
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Item item = (Item) objAdapter1.getItem(position);
Intent intent = new Intent(ChooseDriver.this, DriverDetail.class);
intent.putExtra(ID, item.getId());
intent.putExtra(NAME, item.getName().toString());
intent.putExtra(IMAGE, item.getImage().toString());
image.buildDrawingCache();
Bitmap image= image.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);
}
});
try this:
Main Activity:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mItem.getIcon().compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
newIntent.putExtra("image", byteArray);
Second Activity:
byte[] byteArray = getIntent().getExtras().getByteArray("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
img.setImageBitmap(bmp);
In FirstActivity,
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("YourBitmap", bitmap);
and in SecondActivity,
Intent intent = getIntent();
Bitmap bitmap = (Bitmap)intent.getParcelableExtra("YourBitmap");
May it help you.