Skip to content Skip to sidebar Skip to footer

How To Take A Picture Of One Linearlayout In Android Studio?

I would like take a picture of a part of activity sub layout. so.. activity itself will be FrameLayout and FrameLayout includes some other views like Toolbar, ImageView, TextView e

Solution 1:

//Define a bitmap with height and width of the ViewBitmapviewBitmap= Bitmap.createBitmap(v.getWidth(),v.getHeight(),Bitmap.Config.RGB_565);

CanvasviewCanvas=newCanvas(viewBitmap);

//get background of canvasDrawablebackgroundDrawable= v.getBackground();

if(backgroundDrawable!=null){
backgroundDrawable.draw(viewCanvas);//draw the background on canvas;
}
else{
viewCanvas.drawColor(Color.GREEN);
//draw on canvas
v.draw(viewCanvas) 
}

//write the above generated bitmap  to a fileStringfileStamp=newSimpleDateFormat("yyyyMMdd_HHmmss").format(newDate());
        OutputStreamoutputStream=null;
        try{
            imgFile = newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),fileStamp+".jpg");
            outputStream = newFileOutputStream(imgFile);
            b.compress(Bitmap.CompressFormat.JPEG,40,outputStream);
            outputStream.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }

Don't forget to add this permission

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Solution 2:

You complete full source like..

privatevoidsnapScreen() {
    Viewv1= getWindow().getDecorView().getRootView();
    // View v1 = iv.getRootView(); //even this worksViewv1= findViewById(R.id.main_result); //this works too for particular view// but gives only content
    Bitmap myBitmap;
    v1.setDrawingCacheEnabled(true);
    myBitmap = v1.getDrawingCache();
    snap.saveBitmap(myBitmap, IntentResultItem);
}

For create Snapshot

Make sure you give permission for external storage

publicclassSendSnap {

Context mContext;

publicSendSnap(Context context) {
    this.mContext = context;
}


publicvoidsaveBitmap(Bitmap bitmap, String msg) {

    StringfilePath= Environment.getExternalStorageDirectory()
            + File.separator + "screenshot.png";
    FileimagePath=newFile(filePath);
    FileOutputStream fos;
    try {
        fos = newFileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

}

Post a Comment for "How To Take A Picture Of One Linearlayout In Android Studio?"