Taking screenshot of a view gives black image? - android

I tried to take a screenshot of a imageview programatically! When the image is saved to folder it is producing black image! when i tried to take screenshot of my entire scroll view, I am able to take screenshot. Please help:
my Code is:
private void saveop(){
RelativeLayout parent = (RelativeLayout) findViewById(R.id.opimagelayout);// which includes your view or buttons?
Bitmap bitmap = Bitmap.createBitmap(384,
100, Bitmap.Config.ARGB_8888);
Canvas bitmapHolder = new Canvas(bitmap);
parent.draw(bitmapHolder);
try {
final File f = savebitmaps(bitmap,"operator","");
} catch (IOException e) {
e.printStackTrace();
}
}
public static File savebitmaps(Bitmap bmp,String transno, String currentDateandTime) throws IOException {
Bitmap out = BITMAP_RESIZER(bmp, 384,112);
File folder = new File(Environment.getExternalStorageDirectory() + "/MobeeLoad");
File file = new File(folder, transno+".jpg");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
out.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
bmp.recycle();
out.recycle();
} catch (Exception e) {
}
bmp.recycle();
return file;
}
Layout xml:
<RelativeLayout
android:id="#+id/opimagelayout"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_below="#+id/date"
android:layout_height="90dp">
<ImageView
android:id="#+id/opimage"
android:layout_width="90dp"
android:src="#drawable/check"
android:layout_centerHorizontal="true"
android:layout_height="90dp" />
</RelativeLayout>

Related

how to get whole bitmap of all view of frame layout which is in scrollView

I am trying to create bitmap of all view.but I am not able to create bitmap of all view,please help me to resolve this problem.
public Bitmap overlay(FrameLayout frame_layout){
frame_layout.setDrawingCacheEnabled(true);
frame_layout.buildDrawingCache();
return frame_layout.getDrawingCache();
}
public void saveImage(){
Bitmap bitmap=overlay(print_layout);
try{
String path = Environment.getExternalStorageDirectory().toString()+ File.separator+"bitmap";
File path_file=new File(path);
path_file.mkdirs();
OutputStream fOut = null;
File file = new File(path+ File.separator+"print.png"); // the File to save to
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush();
fOut.close();
Log.d("sun","saved");
}
catch (Exception e){
Log.d("sun",e.toString());
}
}

Extra padding on Bitmap created from a view

I am having an issue where I create a Bitmap from a View by calling view.getDrawingCache(); The bitmap is almost perfect but when I set the bitmap to an ImageView there is some added "space" or "padding" to the top and bottom of the Bitmap.
Here is how I am capturing the view. The view in question has many different views added on top of it to make a sort of stacked effect.
Is there a way to set the bounds that the bitmap is capturing? or to remove this padding that is added?
What I have found
It appears as if the extra padding is being added due to the "fill_parent" attributes and the "android:scaleType:fitCenter". When I change the scaleType to fitXY the image no longer has padding. The only problem I have with that is that it stretches the image and does not maintain the aspect ratio required.
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = view.getDrawingCache();
Here is the code that I am calling to save the bitmap
private void saveToInternalStorage(Bitmap bitmapImage) {
ContextWrapper cw = new ContextWrapper(this);
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Pictures" + File.separator + "P"
+ File.separator);
if (!root.exists()) {
root.mkdirs();
Log.e("ROOT", "DIRECTORY DOES NOT EXIST");
} else {
Log.e("ROOT", "DIRECTORY EXISTS");
File sdImageMainDirectory = new File(root, "p_"
+ (System.currentTimeMillis() / 1000L) + ".png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(sdImageMainDirectory);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
Intent intent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(sdImageMainDirectory));
this.sendBroadcast(intent);
Log.e("DIRECTORY", "" + sdImageMainDirectory.getAbsolutePath());
} catch (NullPointerException e) {
} finally {
Intent shareIntent = new Intent(this, SubmissionActivity.class);
shareIntent.putExtra("URI", Uri.fromFile(sdImageMainDirectory));
startActivity(shareIntent);
finish();
}
// change activity to something to share the picture
}
}
XML for the layout containing the ImageView
<FrameLayout
android:id="#+id/root_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageViewEdit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/abc_ab_solid_dark_holo" />
</FrameLayout>

Sharing photo at xml view

i want to share a photo that is at my xml view but any sharing intenet requires that photo should be saved on memory card. how can i use that photo?
this is my xml:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="17dp"
android:text="Button" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ellyfish" />
You can try something like this:
First get the bitmap from imageview:
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
Create a new file using this bitmap:
public File createImageFromBitmap(Bitmap bitmap) {
File f ;
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "rwd_temp.jpg");
f.createNewFile();
// write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return f;
}
What I am doing is getting the bitmap from the imageview and saving that bitmap in a file named rwd_temp.jpg. After you have the file you can upload it to server or share it.
You will require WRITE_EXTERNAL_STORAGE permission for this.

Get the image after rotating from ImageView

In this question ImageView will reset to original state after rotating, I know how to rotate an imageView and keep it after rotating.
But now, I need to do some scaling based on the rotated image. When scaling, the image will reset to original one before rotating, this is not what I want.
So I want to get the bitmap from ImageView after rotating, and let the ImageView use the new one. The question is:
How to get the bitmap from ImageView after rotating?
To get bitmap from imageview:
imageview.buildDrawingCache();
Bitmap bm=imageview.getDrawingCache();
To save it in a file:
OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}

screenshot but creates blank file : android?

I am working on android. I have following code to capture a screenshot.
Setting up your Root layout:
View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
Function to get the rendered view:
private void getScreen()
{
View content = findViewById(R.id.layoutroot);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/test.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
My problem is that it creates a zero kb file. what could be problem?
do these changes and try again:
make the content variable a final and call findViewById on the layout of the activity
final View content = somelayout.findViewById(R.id.layoutroot);
You have not write data to the file. you are directly closing the file thats why you got a file with 0 kb
use something like this
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
byte[] mydata = null;//data in byte array
ostream.write(mydata);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}

Categories

Resources