Save Layout as Bitmap - android

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));
}
});

Related

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?

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;
}

Image is not fullscreen

I want to display an image to be fullscreen :
public class PhotoPleinEcranActivity extends Activity {
private Bundle data;
private ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.photo_plein_ecran);
img = (ImageView)findViewById(R.id.zoom);
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = true; //Disable Dithering mode
bfOptions.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage = new byte[32 * 1024];
data = getIntent().getExtras();
FileInputStream fs = null;
Bitmap bm;
try {
fs = new FileInputStream(new File(data.getString("path")));
if(fs != null) {
bm = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
img.setImageBitmap(bm);
}
} catch (IOException e) {
} finally {
if(fs != null) {
try {
fs.close();
} catch (IOException e) {
}
}
}
}
}
The layout :
<?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="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:orientation="horizontal">
<ImageView
android:id="#+id/zoom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
/>
</LinearLayout>
Manifest file :
<activity
android:name="com.ambre.impots.PhotoPleinEcranActivity"
android:label="#string/galleryPhotos"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
</activity>
At runtime there is a grey field at the bottom of the image ( I surrounded the grey field by a yellow color ) :
So how to make that grey field disappear ?
This will solve your problem.
Add below line in ImageView.
android:scaleType="fitXY"
add android:scaleType="fitXY" to your ImageView, fitXY, will fill width and height independently to match the destination
add property scaleType to fitXY like this
<ImageView
android:id="#+id/zoom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:scaleType="fitXY" />
Change your image view like this
<ImageView
android:id="#+id/zoom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dip"
android:layout_marginTop="0dip"
android:adjustViewBounds="true"
android:paddingBottom="0dip"
android:paddingTop="0dip"
android:scaleType="fitXY" />

save multiple image views as a single file

In my application , one Relative Layout has totally three ImageView()'s . I want to save all the three imageviews as a single .png file to the device storage on a single button click.
My code:
Layout XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/make" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_above="#+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/am0" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Save Meme"
android:textStyle="bold"
android:textColor="#color/wt" />
<ImageView
android:id="#+id/top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="64dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="74dp"
android:src="#drawable/ic_launcher" />
Activity Code :
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sm);
main1 = (ImageView) findViewById(R.id.imageView1);
top = (ImageView) findViewById(R.id.top);
down = (ImageView) findViewById(R.id.down);
Bundle rec = getIntent().getExtras();
int rec1 = getIntent().getExtras().getInt("ams0");
Bitmap bmp = (Bitmap) rec.getParcelable("bm0");
Bitmap bmp1 = (Bitmap) rec.getParcelable("bm1");
main1.setImageResource(rec1);
top.setImageBitmap(bmp);
down.setImageBitmap(bmp1);
}
How do I save the image views as a file ?
Try using a Сanvas
Bitmap mainBitmap = ((BitmapDrawable)main1.getDrawable()).getBitmap().copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mainBitmap);
canvas.drawBitmap(bmp.copy(Bitmap.Config.ARGB_8888, true), 1, 1, null);
canvas.drawBitmap(bmp1.copy(Bitmap.Config.ARGB_8888, true), 1, 1, null);
OutputStream os = null;
try {
os = new FileOutputStream("/sdcard/DCIM/Camera/myImages.png");
mainBitmap.compress(Bitmap.CompressFormat.PNG, 50, os);
} catch (IOException e) {
e.printStackTrace();
}

combining two bitmaps into one bitmap throws an OutOfMemoryException

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?

Categories

Resources