I have to print the entire text of a text field into a picture. The reason is: I have to exchange messages with unsupported UTF-8 characters between Android and other web clients. By unsupported UTF-8 characters I mean missing fonts in Android (see this topic here ). I tried to use the direct way
Bitmap b;
EditText editText = (EditText) findViewById(R.id.editText);
editText.buildDrawingCache();
b = editText.getDrawingCache();
editText.destroyDrawingCache();
which works like a charm until I have multiple lines: The solution captures only the text which is visible to the user instead of the complete text inside of a long text field (scrollbars!).
I also tried another workaround by generating a picture from a stackoverflow answer. This prints the entire text but does not respect text formatting like newlines. But I don't want to handle all the stuff by myself.
I am forced to use Android 4.3 and earlier versions.
Is there smart way to capture text into pictures? If not:
Is it possible to modify the code above to get it work as expected?
After searching for another 24 hours for a solution I ran into this solution for a Webview. The trick is
generate another View to hold the content to avoid the marker for EditText on the lower edge of view which will be also printed into the picture
copy the Bitmap to avoid problems with software renderer after destroyDrawingCache() when trying to use Bitmap.compress().
Here is the code:
EditText editText = (EditText) findViewById(R.id.editText);
TextView textView = new TextView(this.getApplicationContext());
textView.setTypeface(editText.getTypeface());
textView.setText(editText.getText());
textView.measure(
View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
textView.setDrawingCacheEnabled(true);
textView.buildDrawingCache();
Bitmap b = textView.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
textView.destroyDrawingCache();
try{
String path = Environment.getExternalStorageDirectory().toString() + "/picture.png";
OutputStream outputStream = new FileOutputStream(new File(path));
b.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Related
I have a notification listener service that reads notifications from other apps (with the user's permission) and extracts all the data. Able to access everything except the image shown in the expanded view of the notification.
I am also reading the EXTRA_PICTURE intent value
if (extras.containsKey(Notification.EXTRA_PICTURE)) {
// this bitmap contain the picture attachment
try {
Bitmap bmp = (Bitmap) extras.get(Notification.EXTRA_PICTURE);
ByteArrayOutputStream picStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, picStream);
byte[] picBmpData = picStream.toByteArray();
notificationPicture = Base64.encodeToString(picBmpData, Base64.NO_WRAP);
} catch(Exception e){
e.printStackTrace();
}
}
This works sometimes but for all notifications with image in their expanded view. Am I missing anything?
UPDATE: more clarification on what happens in the negative cases:
In negative cases when there is an image in the notification, the extras dont seem to have the EXTRA_PICTURE key set. However I do see
android.template
key set to
android.app.Notification$BigPictureStyle
I am trying to export TeeChart as Image on Xamarin Android without showing the chart. It creates the file on external storage. However, the file is broken and cannot be loaded.
Do you know if it is possible? If it possible, can you give me a sample code doing that?
You can use this to get the view's bitmap and storage it:
Steema.TeeChart.TChart tChart1= new Steema.TeeChart.TChart(this);
Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar();
tChart1.Series.Add(bar1);
bar1.Add(3, "Pears", System.Drawing.Color.Red);
bar1.Add(4, "Apples", System.Drawing.Color.Blue);
bar1.Add(2, "Oranges", System.Drawing.Color.Green);
Steema.TeeChart.Themes.BlackIsBackTheme theme = new Steema.TeeChart.Themes.BlackIsBackTheme(tChart1.Chart);
theme.Apply();
// here you can get the view' bitmap
tChart1.DrawingCacheEnabled = true;
tChart1.BuildDrawingCache();
Bitmap viewBitmap = tChart1.DrawingCache;
//storage the bitmap.
FileStream stream = File.OpenWrite(FilesDir.AbsolutePath + "111111.jpg");
viewBitmap.Compress(Bitmap.CompressFormat.Jpeg, 90, stream);
Here is the setDrawingCacheEnabled method, which allow you get view's bitmap.
A moment when I click a notification bar, I want app to capture current screen. But i want the app capture 'after the notification bar disappears'. And I want that captured screen to be saved and loaded right after and showed up. How shoud i do?
For example code just do template of "new full screen activity" this will show you how to show/hide toolbar and make full screen.
Then just modify the code so it doesn't happen on a timer, instead move it to a touch listener that you place on the toolbar itself.
Lastly, once the fade has disappeared (which you can do instant if you want) then simply make your own screenshot. There are plenty of great libraries that will do this for you if you want or you can use simple code that I put in a class I named ImageHelper like this:
public static Bitmap takeScreenshotOfView(Activity context, Bitmap.CompressFormat compressFormat){
Bitmap screenshot = null;
try {
// create bitmap screen capture
View v1 = context.getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(context.getFilesDir() + File.separator + "A35_temp" + File.separator + "screenshot_temp");
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
screenshot.compress(compressFormat, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
return screenshot;
}
And just use it like:
Bitmap screenshot = ImageHelper.takeScreenshotOfView(this, Bitmap.CompressFormat.JPEG);
Next start a fullScreen Activity new for Preview but default with toolbar hidden and fullscreen ImageView with your share overlay buttons or whatever you want at the bottom.
I am trying to create a custom keyboard and use "SoftKeyboard" sample in android SDK for it. I did few modifications with that sample and created my custom keyboard. I can use this custome keyboard with default Messaging app of my Android device.
Now I want to click a button in my custom keyboard and add an image when I type a SMS. I noticed that there is String Builder in "SoftKeyBoard.java" class (private StringBuilder mComposing = new StringBuilder()) and it is appended chars when we type letters using keyboard.
I tried to append an image of my SD card like below,
String imageDataString = "";
String path = Environment.getExternalStorageDirectory().toString() + "/SamplePictures/";
File file = new File(path, "myimage.jpg");
try {
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
// Converting Image byte array into Base64 String
imageDataString = encodeImage(imageData);
imageInFile.close();
} catch (FileNotFoundException e) {
System.out.println("Image not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the Image " + ioe);
}
and I appended "imageDataString" to String builder like below,
mComposing.append(imageDataString);
But I got so many characters, not an image.
Is it possible to insert an image when I type SMS using my keyboard?
Updated : I used ImageSpan and Spannable with following code.
SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's a my picture " );
Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.bitmap );
ssb.setSpan( new ImageSpan( smiley ), 16, 17,Spannable.SPAN_INCLUSIVE_INCLUSIVE );
mComposing.append(ssb);
But it displays only "Here's a my picture" and no image. I created a sample separate app with an EditText and set above "ssb" variable as the text of that EditText. Then it displays well the image. But it doesn't work with Messaging app. If I can set the Messaging app EditText, I guess I can set the image.
Is there any way to access and update the Edit text of Messaging app?
Thanks in Advance..!!
I think what you want to do is use an ImageSpan added to a Spannable, a solution which is already described here. After pressing the image button on your keyboard, you'll fire of a method that should update the editText by taking the existing text from it, adding an ImageSpan containing your image and setting that back to the editText.
I am downloading a bitmap from a website and then displaying it in my application. Whenever I download this image and set it into an ImageView, there is always a lot of extra space above or below the actual image. This extra space is part of the ImageView and is only there after I set the ImageBitmap to the downloaded bitmap.
So, this is making me think that the extra space is somehow part of the bitmap.
However, when I download the same image in a Webview, there is no extra space.
If you have any ideas on why this could be happening, please let me know! Let me know if you need any more information, thanks.
Edit: Here's my code getting the bitmap:
InputStream in = null;
Message msg = Message.obtain();
msg.what = 1;
try{
in = openHttpConnection(_url);
if (in != null)
{
Bitmap bit = BitmapFactory.decodeStream(in);
Bundle b = new Bundle();
b.putParcelable("bitmap", bit);
msg.setData(b);
in.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
_handle.sendMessage(msg);
And this is what I use to then for the ImageView, I get the bitmap from the code above and:
imageV.setImageBitmap(comic);
Edit 2:
After trying this with some other images from different website, I've found that this white space is not always there. Given that, and there's probably not anything wrong with the code, are there any suggestions on removing this extra space since it doesn't show up in the actual image online nor in a webview?
imageSize = din.readInt();
imageName = din.readUTF();
byte b[] = new byte[imageSize];
din.readFully(b);
bmImg = BitmapFactory.decodeByteArray(b,0,b.length);
//This works for me....
//din is DataInputStream object.