Error in screenshot (Android) - android

I am using the following code to take a screenshot of a TableLayout. I must add that the theme of my android application is set to LIGHT. It shows perfectly fine on the emulator screen (just like it is supposed to)....however, once the screenshot is taken, the image turns out to be like this...can anyone help point out what I am doing wrong here? Thanks!
private void getScreen()
{
View content = findViewById(R.id.TransactionLog);
content.setDrawingCacheEnabled(true);
content.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());
content.setDrawingCacheEnabled(false); // clear drawing cache
File file = new File(Environment.getExternalStorageDirectory() +
File.separator + "logDetails.jpeg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.flush();
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
if (!file.exists()) {
sendmail();
}
}

1) you should use png. Jpeg is for photos (basically).
2) because you're using Jpeg, I assume those black areas would otherwise be transparent. Png supports transparency. Jpeg does not. I have not tried drawingCache, so maybe it doesn't support transparency either and I'm totally wrong, but that's my guess.
So, summary, try Png.

Related

Correctly managing color of bitmap image without creating anomalies on android device

Working on Android fashion app and downloading various images from AWS Cloudfront, then storing them locally on internal storage.
The same image looks different if I show it from my app or from the gallery app. It's not about display, I'm sure of this because I tested with the Photoshop color picker.
I guess it may depend on compression but I have maximum value (100). Also, I tried to write directly array byte to my memory and decode the array without any compression.
Look this image to better understand:
I know that Android handles different color profile and I've tried many of those, with no effects.
Also, I use Glide library to load the images and I set no cache and ARGB8888 color profile.
Then I tried to use Picasso, but nothing changed.
That's how I download and save the images:
#Override
protected Bitmap doInBackground(String... strings) {
try {
return BitmapFactory.decodeStream((InputStream)new URL(strings[0]).getContent());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
#Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null) {
try {
String filename = colorImage.getUniqueId() + "_zoom.jpg";
// I tried this
File file = new File(getContext().getFilesDir(), filename);
FileOutputStream fos = new FileOutputStream(file, false);
// Writing the bitmap to the output stream
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// But also this
// File file = new File(getContext().getFilesDir(), filename);
// FileOutputStream fos = new FileOutputStream(file, false);
// FileUtils.writeByteArrayToFile(file, array);
fos.close();
} catch (Exception e) {
Log.e("mylog", "saveInternalStorageError(): " + e.getMessage());
}
}
}
That's how I show the image (after I read all issues referenced here https://github.com/bumptech/glide/issues/515):
Glide.with(this)
.load(imageUriF)
.asBitmap()
.encoder(new BitmapEncoder(Bitmap.CompressFormat.JPEG, 100))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(firstImage);
I also tried to show the image without Glide with same results
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
img.setImageBitmap(myBitmap);
And Picasso too, with same results.
Have you got any idea about this issue?

bitmap.compress destroys the picture quality

I'm using an app to get the gps location and draw it as a circle on a bitmap then save it to proceed, so I need repetitively to read and save the file. But unfortunately when I save the file and read it, the file is damaged after some iterations...! the code:
File output = new File(tmpDirectory, "map.jpg");
try {
OutputStream outputStream = new FileOutputStream(output);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
Message("error!");
}
directory = tmpDirectory;//updating directory to load the manipulated image
readFile(directory + "map.jpg", false);//setting the image view new image
image included:picture after iterations
image included:
main image
JPEG uses lossy compression. That means with each iteration you will lose some quality. You should use loseless format like PNG if you want to preserve it.
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

to take screenshot and to draw on it (android)

I want to take screenshot of my WebView by clicking on menu item . And then I want to draw on this screenshot and save it on my sdcard.
I've also found the solution of taking screenshot and saving. Here it is:
private void getScreen(View content) {
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/screen.png");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and on onOptionsItemSelected() method:
View content = findViewById(R.id.webview_main);
content.setDrawingCacheEnabled(true);
getScreen(content);
It`s work ok, but I want to implement drawing on it too. If anyone know, how to do this, tell me please,
I will be very grateful.

Capturing the snapshot of the Relative layout with transparent background gives a bitmap with black background not transparent

I have a Relative layout with transparent background which contains images,text etc when i capture its bitmap by using Drawing cache or canvas the transparent color get converted to black i have tried lots of means but no success...
rl_mainCanvesLayout.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
Bitmap bitmap =Bitmap.createBitmap(rl_mainCanvesLayout.getDrawingCache());
rl_mainCanvesLayout.setDrawingCacheEnabled(false);
Bitmap bitmap = Bitmap.createBitmap(rl_mainCanvesLayout.getDrawingCache());
private String savebitmap(Bitmap bitmap, String filename) {
String dir = Environment.getExternalStorageDirectory() + File.separator + "abc";
File file_dir = new File(dir);
file_dir.mkdir();
FileOutputStream outStream = null;
File file = new File(dir, filename + ".jpg");
if (file.exists()) {
file.delete();
file = new File(dir, filename + ".jpg");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file.toString();
}
According to the source of the getDrawingCache() method, it ends up in buildDrawingCache() after a few calls, this is where the real work is done. Here, it creates a bitmap then draw it by a canvas. In your situation you set transparent color, which is equal to 0, as for this, it does NOT set the returned bitmap's color map(at line 14450 eraseColor not called), it leaves it at default value. Later as it draws your layout, it does NOT take care about ANYTHING except itself and it's children. As consequence of this, you get the black background as those pixels are not assigned.
A possible workorund for this, is to set different drawingCacheBackgroundColor, which value is not 0, or set a color attribute for the relative layout. By these, you can get the desired background color.
If your intention was to get a snapshot of the layout and it's real background(other views, activty etc.), then you should get drawing cache from it's parent and apply it to this layout's drawing cache with image editing or just simply take a screenshot and crop the layout's area.

Black portion in place of transparent portion while saving the bitmap

I am frustrated searching almost every google page for this problem.
I need to save my bitmap to file.
I have used this method several times with no problem at all. But now I am having a problem.
The Bitmap I am saving is a round image with transparency and having .png format which I have placed in res folder. But I am getting black portion in place of transparent portion.
This is the code I am using for saving the bitmap.
void saveImage(Bitmap bmp) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bytes);
finalImg = new File(Environment.getExternalStorageDirectory(),
"emoticon_temp" + ".png");
finalImg.createNewFile();
FileOutputStream fo = new FileOutputStream(finalImg);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am saving this bitmap as file because I need to share this image and for sharing the image I need the Stream Uri.
If you got any other Idea for sharing a bitmap, please let me know.

Categories

Resources