Android Save View to Jpg or Png

Android save view to jpg or png

You can take advantage of a View's drawing cache.

view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));

Where view is your View. The 95 is the quality of the JPG compression. And the file output stream is just that.

How to save the layout view as image or pdf to sd card in android?

Add permission in the manifest file

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

Use the code below

LinearLayout content = findViewById(R.id.rlid);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file,f;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
file =new File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache");
if(!file.exists())
{
file.mkdirs();

}
f = new File(file.getAbsolutePath()+file.seperator+ "filename"+".png");
}
FileOutputStream ostream = new FileOutputStream(f);
bitmap.compress(CompressFormat.PNG, 10, ostream);
ostream.close();

}
catch (Exception e){
e.printStackTrace();
}

How to save an image from ImageView?

Try this..

Change root.createNewFile(); to cachePath.createNewFile();

File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}

EDIT:

FileOutputStream ostream = new FileOutputStream(cachePath);

Saving image from image view into internal/external device storage

try this code in your onActivityResult()

if(isStoragePermissionGranted()){
SaveImage(bitmap)
}

For saving the image:

 private void SaveImage(Bitmap finalBitmap) {

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();

} catch (Exception e) {
e.printStackTrace();
}
}

use this method for permission check:

    public  boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {

Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}

For getting the result:

 @Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(getContext(), "Permission granted", Toast.LENGTH_SHORT).show();
} else {

// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}

case 2: {

if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getContext(), "Permission granted", Toast.LENGTH_SHORT).show();
SaveImage(bitmap);
} else {
Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}

// other 'case' lines to check for other
// permissions this app might request
}
}

Convert Any view into image and save it

try this to convert a view (framelayout) into a bitmap:

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

then, save your bitmap into a file:

try {
FileOutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

don't forget to set the permission of writing storage into your AndroidManifest.xml:

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

How can i save a view as a image in gallery?

Make your LinearLayout as setDrawingCacheEnabled(true); and capture the layout and convert it into the Bitmap and save it as an image in your sdcard.

Write below code in your Button click.
Try out below code.

public class LayoutDisplay2 extends Activity{

LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.layout2);
ll=(LinearLayout)findViewById(R.id.linearlayout);

//Add button in your layout and write the below code onclick of button.
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

ll.setDrawingCacheEnabled(true);
Bitmap bitmap = ll.getDrawingCache();

String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/saved_picture");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = n + ".jpg";
File file = new File(newDir, fotoname);
String s = file.getAbsolutePath();
System.err.print("Path of saved image." + s);

try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {

}
}
});

in the manifest:

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


Related Topics



Leave a reply



Submit