combining two bitmaps into one bitmap throws an OutOfMemoryException - android

I am using the code below to combine two bitmaps into one bitmap:
real = (RelativeLayout)findViewById(R.id.real);
apply.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
loadBitmapFromView(real).compress(Bitmap.CompressFormat.PNG, 100, bs);
byte[] byteArray = bs.toByteArray();
i.putExtra("bittext", byteArray);
setResult(RESULT_OK,i);
finish();
}
});
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg">
<RelativeLayout
android:id="#+id/real"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="25dp"
android:layout_above="#+id/text"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"enter code here
android:id="#+id/imagetext"
android:scaleType="matrix"/>
</RelativeLayout>
<Button android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/text"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"/>`enter code here`
<Button
android:id="#+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/text"
android:layout_margin="10dp"
android:background="#drawable/apply" />
</RelativeLayout>
Error occurred when the #+id/apply button is clicked, it can not go to the previous activity along with the bitmap. this problem was occurred only one device (Videocon mobile only). What is the issue, and how do I fix it?

Related

How to share specific 1 card view of activity from Android App programmatically like Google Analytics App

I am working on an android app of social network and i am stuck at one place.
I want to share specific card view from an activity to facebook or whatsapp wherever user wants to share as an image like google analytics app.
check screenshot below
Suppose i want to share top 2 card then i can share like this to whatsapp or fb whever i want
First card
Second card
First i thought to take screenshot of app and share it to user's social app but it doesn't match to requirements and i have no idea how can we achieve this.
Help me to solve this problem.
You cannot share a 'Card' to other Apps, however you share a Bitmap (Screenshot) of the Card...
Here's how to do it -
Create a method like below -
public Bitmap ViewShot(View v) {
int height = v.getHeight();
int width = v.getWidth();
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas (b);
v.layout(0, 0 , v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
Pass the CardView in the View parameter of ViewShot like -
Bitmap screenshot = ViewShot(myCardView);
Now you can save that Bitmap to a Temp file & share it with other app!
Create an image and sent it via intent. An easy way to create a bitmap of a view is to create a bitmap object with width and height equal to that of the view, create a Canvas for that bitmap, and call the view's onDraw function passing in that Canvas. That will make the View draw itself to the Bitmap.
If you want to share specific data of that card, my suggestion is Firebase Dynamic Links. I think this is what you are looking for. You can create a dynamic link for specific page of your app and share the link via whatever you want.
I was working on a Project where my task was to create a business card using XML and share the card on any social media app or mail
Here's the code that worked for me.
public class BusinessCard extends AppCompatActivity {
Button share;
CardView cardView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_business_card);
share=findViewById(R.id.share);
cardView=findViewById(R.id.visitingCard);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Text",Toast.LENGTH_SHORT).show();
Bitmap screenshot = ViewShot(cardView);
}
});
}
public Bitmap ViewShot(View v) {
int height = v.getHeight();
int width = v.getWidth();
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas (b);
v.layout(0, 0 , v.getLayoutParams().width=750, v.getLayoutParams().height=700);
v.draw(c);
Intent intent =new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "Title", null);
Uri imageUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent, "Select"));
return b;
}
}
XML layout(Code):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#color/purple_200">
<androidx.cardview.widget.CardView
android:id="#+id/visitingCard"
android:backgroundTint="#BCB14F"
android:layout_width="400dp"
android:layout_height="350dp"
app:cardElevation="10dp"
app:cardCornerRadius="30dp"
app:cardBackgroundColor="#color/gray"
app:cardPreventCornerOverlap="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:src="#drawable/e_removebg_preview"
android:layout_width="100dp"
android:layout_height="100dp">
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:textStyle="bold"
android:text="User Name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="10dp"
android:textStyle="bold"
android:text="CEO"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Number :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="10dp"
android:text="+919876543210"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="email :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:text="www.googleuser#gmail.com"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="website :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:text="userwebsite.com"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3790 Las Vegas Blvd, South"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Las Vegas,Nevada ,United Status, 897845"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<Button
android:id="#+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share">
</Button>
</LinearLayout>
I hope this will be helpful to you

capture frame layout has this issue

this is my xml file:
<RelativeLayout
android:id="#+id/relativeLayoutTemplateMain"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/LinearLayouttools"
android:layout_above="#+id/lltexttools"
android:layout_gravity="center">
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="centerInside" />
</FrameLayout>
</RelativeLayout>
i capture framelayout with this code:
fOut = new FileOutputStream(file);
framelayout.setDrawingCacheEnabled(true);
Bitmap bitmap = framelayout.getDrawingCache();
bitmap.compress(Bitmap.CompressFormat.jpeg, 100, fOut);
fOut.flush();
fOut.close();
but Around of the picture is black:
http://uupload.ir/files/z8cv_smsbaaz.ir_4.jpg
also when i set scaleType="FitXY":
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="FitXY"/>
image scale(i dont want scale image)
how i solve this problem?
you can use below method to create view to Bitmap .
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
Converting a view to Bitmap without displaying it in Android?

Exception: android.widget.LinearLayout cannot be cast to android.widget.ImageView

I need to set the images to grid view dynamically. but i'm getting exception: android.widget.LinearLayout cannot be cast to android.widget.ImageView
I need to get the view of the grid without onclick. So please help me.
private void setTickMark(String icon_name_val) {
ImageView imageView = (ImageView) rootView;
//getting clicked image
String iconimg = icon_name_val;
String iconimgname = iconimg;
// int idval = getResources().getIdentifier(iconimgname, "drawable",getActivity().getPackageName());
// Drawable drawable = getResources().getDrawable(idval);
// Log.d("dra",String.valueOf(drawable));
int resID = getResources().getIdentifier(iconimgname, "drawable", getActivity().getPackageName());
Bitmap bitmapbus = BitmapFactory.decodeResource(getResources(), R.drawable.bus);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID);
Bitmap resized = Bitmap.createScaledBitmap(bitmap1, bitmapbus.getWidth(), bitmapbus.getHeight(), true);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.tick);
// Bitmap resultBitmap = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap resultBitmap = Bitmap.createBitmap(bitmapbus.getWidth(), bitmapbus.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(resultBitmap);
c.drawBitmap(resized, 0, 0, null);
c.drawBitmap(bitmap2, 0, 0, null);
imageView.setImageBitmap(resultBitmap);
}
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/popup"
android:layout_height="0dp"
android:layout_weight=".90"
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_marginTop="20dp">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:columnWidth="60dp"
android:numColumns="auto_fit"
android:verticalSpacing="20dp"
android:horizontalSpacing="20dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="4dp"
android:duplicateParentState="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/save"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/cancel"/>
</LinearLayout>
</LinearLayout>
Add an imageview to your gridview
then only define ImageView imageView = (ImageView) rootView.findViewById(R.id.ImageView1);

Android: Take Screen Shoot for a layout

I am trying to create a PhotoFrame app, for that i have a frame and i add an image to that frame from gallery.This Frame is in one of the layout in the main layout am trying to take screen shoot for that layout.
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root"
tools:context="com.ccs.photoframe.MainActivity"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:orientation="vertical">
<Button
android:id="#+id/btn_select"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Select Photo"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".8"
android:background="#drawable/photo_frame"
android:orientation="vertical"
>
<ImageView
android:id="#+id/img_save_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginBottom="70dp"
>
<ImageView
android:id="#+id/ivImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_save_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Save Photo"/>
</LinearLayout>
I am trying to take screen shoot in the LinearLayout "layout_frame"
I tried ...
btn_save_photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View v1 = layout_frame.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
img_save_photo = (ImageView) findViewById(R.id.img_save_photo);
img_save_photo.setVisibility(View.VISIBLE);
img_save_photo.setBackgroundDrawable(bitmapDrawable);
}
});
But this taking screen shoot for the entire layout.
Is there any possible way to take screen shoot for the specific location.
Thanks in advance:)
Yes you can get screen shot of any view like this.
ImageView redoo_btn = (ImageView) findViewById(R.id.redoo_btn);
FrameLayout paintLayout = (FrameLayout) findViewById(R.id.drawing);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.drawing1);
Bitmap bm = Takeshot(linearLayout);
Bitmap bm = Takeshot(redoo_btn);
Bitmap bm = Takeshot(paintLayout);
Bitmap Takeshot(View view) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
// view.setImageBitmap(b);
return b;
}
Yes you can do this way:
LinearLayout mlinearlaoytfrme=(LinearLayout)findViewById(R.id.layout_frame);
Bitmap mBItmap=takeScreenshotReceipt();
img_save_photo = (ImageView) findViewById(R.id.img_save_photo);
img_save_photo.setVisibility(View.VISIBLE);
img_save_photo.setBackgroundDrawable(bitmapDrawable);
private Bitmap takeScreenshotReceipt()
{
View v1 = getRootView().findViewById(R.id.layout_frame);
v1.setDrawingCacheEnabled(true);
return v1.getDrawingCache();
}
Try this method
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}

Save Layout as Bitmap

I want to save my Linearlayout as an bmp.
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout_QrCode" >
<Space
android:layout_width="15dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Space
android:layout_width="match_parent"
android:layout_height="15dp" />
<ImageView
android:layout_width="185dp"
android:layout_height="185dp"
android:id="#+id/imageView_qrcode" />
<Space
android:layout_width="match_parent"
android:layout_height="15dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="417dp"
android:layout_height="match_parent" >
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="#+id/space2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView_QrSticker_label"
android:layout_alignParentStart="true"
android:layout_above="#+id/space2"
android:textStyle="bold"
android:textSize="40sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView_QrSticker_serial"
android:layout_below="#+id/space2"
android:layout_alignParentStart="true"
android:textSize="30sp" />
</RelativeLayout>
</LinearLayout>
Therefore I use the following code to perform this:
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
But If I run this code my bitmap only contains the ImageView (ImageView_qrcode). I checked it. If I inflate the Linearview the TextViews are shown correctly but the output bmp only contains the ImageView does somebody know why?
You can to try this solution:
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return returnedBitmap;
}
There is solution with drawing cache:
public Bitmap getBitmapFromView(View view){
view.setDrawingCacheEnabled(true);
return view.getDrawingCache();
}
Use setDrawingCacheEnabled(true);
View content = (LinearLayout)findViewById(R.id.linearLayout_QrCode);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/" + yourimagename + ".png");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
content.invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
content.setDrawingCacheEnabled(false);
}
Thanks to #Gil Moshayof it works now!
After calling the getBitmapFromView method via post it works fine
qrCodeSticker.setImageBitmap(((MainActivity) getActivity()).createQrCode(stringForQrCode));
labelSticker.setText(qrDataList[1]);
serialSticker.setText(qrDataList[2]);
qrcodefinal.post(new Runnable() {
#Override
public void run() {
qrcodefinal.setImageBitmap(MainActivity.getBitmapFromView(qrStickerLayout));
}
});

Categories

Resources