Converting FrameLayout from two images into one image - android

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.

Related

How to pass an ImageView which is selected from the internal Storage between Activities

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.

How to share an image which is load from the server using Picasso?

I need help.
I've already fetched an image from the server using web-services, but I don't share this image.
I attached my code, please help me to find the error.
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bitmap bitmap = viewToBitmap(iv, iv.getWidth(), iv.getHeight());
Intent shareintent = new Intent(Intent.ACTION_SEND);
shareintent.setType("image/jpeg");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
File file = new File(Environment.getExternalStorageDirectory() +
File.separator + "Imagedemo.jpg");
try {
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(byteArrayOutputStream.toByteArray());
}
catch (IOException e) {
e.printStackTrace();
}
shareintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/Imagedemo.jpg"));
startActivity(Intent.createChooser(shareintent,"share image"));
}
});
public static Bitmap viewToBitmap(View view, int width, int height){
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Load image using url to Image view is simple
Picasso.get().load(imageUrl).into(imageView);
To share image from imageView using share button use code below
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageVIew.getDrawable());
Bitmap bitmap = bitmapDrawable .getBitmap();
String bitmapPath = Images.Media.insertImage(getContentResolver(), bitmap,"some
title", null);
Uri bitmapUri = Uri.parse(bitmapPath);
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
startActivity(Intent.createChooser(shareIntent,"Share Image"));
}
}
Change this line
shareintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/Imagedemo.jpg"));
To:
shareintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Imagedemo.jpg"));
or better way to get file from external storage is:
new File(Environment.getExternalStorageDirectory() + "/" + "Imagedemo.jpg")
one more thing setType for jpg image is
shareintent.setType("image/jpg");

FAILED BINDER TRANSACTION in Android

Activity1.class
public class CategoryActivity extends BaseActivity implements View.OnClickListener {
private RelativeLayout mReLayout;
private byte[] mImageByte;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
addControl();
addEventOnClick();
mReLayout.setDrawingCacheEnabled(true);
}
private void addEventOnClick() {
findViewById(R.id.layout_category_1).setOnClickListener(this);
}
private void addControl() {
mReLayout = (RelativeLayout) findViewById(R.id.relative_category);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.layout_category_1:
startActivity();
break;
default:
break;
}
}
private void startActivity() {
Bitmap layoutBitmap = Bitmap.createBitmap(mReLayout.getWidth(), mReLayout.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(layoutBitmap);
mReLayout.draw(canvas);
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
layoutBitmap.compress(Bitmap.CompressFormat.PNG, 90, bStream);
mImageByte = bStream.toByteArray();
Intent mIntent = new Intent(Activity1.this, Activity2.class);
mIntent.putExtra(Constant.BYTE_IMAGE, mImageByte);
startActivity(mIntent);
}
}
Activity2.class
OnCreate
byte[] mImageByte=getIntent().getExtras().getByteArray(Constant.BYTE_IMAGE);
Bitmap bmp = BitmapFactory.decodeByteArray(mImageByte, 0, mImageByte.length);
mImageView.setImageBitmap(bmp);
When I click onClickListener. It show bug : FAILED BINDER TRANSACTION !!!
Please. Help me!
I think the reason is you're trying to put a Bitmap as an Intent extra. Although it's theoretically possible, I think the size of the Image limits such possibilities.
If you want to pass an image to another activity, then first
Save the Bitmap to a file
Send the filepath as a String to the next activity
Use the filepath from the next activity to recreate the Bitmap
If that is difficult, then try using a Singleton to store a Bitmap temporarily in memory.
Here's how to save it to disk.
First add the permission in the manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Then...
File file = new File(Environment.getExternalStorageDirectory() + "imageBitmap" + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
Now send the filename to the next activity.
mIntent.putExtra("filename", "imageBitmap");
On the next activity, recreate the bitmap.
String fName = getIntent().getStringExtra("filename");
String path = Environment.getExternalStorageDirectory() + fName + ".png"
Bitmap bm = BitmapFactory.decodeFile(path);
Hope this helps.

cant pass full size image from camera to the next activity

i want to pass an image from one activity to another, this is my code:
public boolean launchCamera(View view) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo;
try {
// place where to store camera taken picture
photo = this.createTemporaryFile("picture", ".jpg");
photo.delete();
} catch (Exception e) {
Log.v(TAG, "Can't create file to take picture!");
Toast.makeText(MainActivity.this, "Please check SD card! Image shot is impossible!", Toast.LENGTH_LONG);
return false;
}
mImageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
//start camera intent
this.startActivityForResult(intent, MenuShootImage);
return true;
}
private File createTemporaryFile(String part, String ext) throws Exception {
File tempDir = Environment.getExternalStorageDirectory();
tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
if (!tempDir.exists()) {
tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
}
public Bitmap grabImage()
{
this.getContentResolver().notifyChange(mImageUri, null);
ContentResolver cr = this.getContentResolver();
Bitmap bitmap=null;
try
{
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
}
catch (Exception e)
{
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Failed to load", e);
}
return bitmap;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
Intent imagepass = null;
Bitmap bitmap = null;
if(requestCode == MenuShootImage && resultCode == RESULT_OK)
{
bitmap = this.grabImage();
imagepass = new Intent(this,MainActivity2.class);
imagepass.putExtra("imagepass", bitmap );
startActivity(imagepass);
}
}
the problem is that i cant reach to the other activity at all, in debug mode i get to the line startactivity(imagepass); and dont go to the MainActivity2.
can somebody help me?
First of all, if you really want to pass your bitmap through Intent, you need to convert it to Byte Array first since Bitmap couldn't be attached with Intent like that.
Here is how to do that https://stackoverflow.com/a/11010565/4651112
But according to the best practices, I suggest you NOT TO SEND A BITMAP THROUGH INTENT AT ALL. Send a filename image and let the target Activity decode it from file. It's much better.
1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.
Convert Bitmap to 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();
Pass byte array into 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);
2) First Save image into SDCard and in next activity set this image into ImageView.
3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity

Passed Bitmap is Not Drawing on Another Activity

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! =)

Categories

Resources