Created
November 1, 2017 17:08
-
-
Save afiqiqmal/cc785bec34a29bbbef678eb3be0702ba to your computer and use it in GitHub Desktop.
This code able to convert the whole view in scrollview to images. It been tested and successfully working.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@BindView(R.id.native_resit) | |
protected ScrollView native_resit; | |
@Override | |
public void onViewCreated(View view, Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
.... | |
.... | |
close_btn.setOnClickListener(new resitClickListener()); | |
print_resit.setOnClickListener(new resitClickListener()); | |
runRecieptData(); | |
} | |
private class resitClickListener implements View.OnClickListener{ | |
@Override | |
public void onClick(View view) { | |
if (view.getId() == R.id.close_btn){ | |
getFragmentManager().popBackStack(); | |
} | |
else if (view.getId() == R.id.print_resit){ | |
print(); | |
} | |
} | |
} | |
private void print(){ | |
ProgressDialog dialog = new ProgressDialog(getActivity()); | |
dialog.setMessage("Saving..."); | |
dialog.show(); | |
Bitmap bitmap = getBitmapFromView(native_resit,native_resit.getChildAt(0).getHeight(),native_resit.getChildAt(0).getWidth()); | |
try { | |
File defaultFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Your_Folder"); | |
if (!defaultFile.exists()) | |
defaultFile.mkdirs(); | |
String filename = "Order ID "+orderHistoryResponse.getOrderId()+".jpg"; | |
File file = new File(defaultFile,filename); | |
if (file.exists()) { | |
file.delete(); | |
file = new File(defaultFile,filename); | |
} | |
FileOutputStream output = new FileOutputStream(file); | |
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); | |
output.flush(); | |
output.close(); | |
dialog.dismiss(); | |
Toast.makeText(getActivity(), Message.RECEIPT_SAVE, Toast.LENGTH_SHORT).show(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
dialog.dismiss(); | |
Toast.makeText(getActivity(), Message.RECEIPT_SAVE_FAILED, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
//create bitmap from the view | |
private Bitmap getBitmapFromView(View view,int height,int width) { | |
Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
Drawable bgDrawable =view.getBackground(); | |
if (bgDrawable!=null) | |
bgDrawable.draw(canvas); | |
else | |
canvas.drawColor(Color.WHITE); | |
view.draw(canvas); | |
return bitmap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment