I am working on Android. Trying to display the "favicon" (aka "shortcut icon") or web pages.
So I already have the code to get the URL of this icon from any website, and download it onto my Android.
Now I am trying to make a Bitmap out of it, but always get null as a result.
The code:
String src = String.format("file:///data/data/com.intuitiveui.android/files/%s",prefName);
// prefName is "facebook.ico", and I do see tht file in my DDMS file browser. It's
// a valid icon file.
Bitmap res = BitmapFactory.decodeFile(src);
// This returns null
TIA
Here is a list of the supported Android media formats. ICO is not among them. You might be able to find a Java-based ICO decoder, though.
SKIA library provides decoder class for ICO file. I was able to display an ICO file in the emulator. Haven't tried it yet in an actual android device though.
Bitmap bmap = BitmapFactory.decodeFile("/sdcard/vlc.ico");
I had a similar problem. BitmapFactory.decode decoded *.ico on emulator but not on my Galaxy S. Solution for me was:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int read=0;
while((read = inputStream.read()) != -1){
bos.write(read);
}
byte[] ba = bos.toByteArray();
Bitmap icon = BitmapFactory.decodeByteArray(ba, 0, ba.length);//new FlushedInputStream(inputStream));
The WebView component has a getFavicon() method so it's definitely possible to decode ICO files in Android. You could have a look at the Android source to see how ICO files are parsed. I've had a quick look but can't find the relevant part.
Alternatively, you should be use the SDK to get favicons for you. However, I've had a quick try and can't get it to work.
For what it's worth here's my test code, noting again that this doesn't work:
String url = "http://stackoverflow.com";
WebView wv = new WebView(this);
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
Log.i("HelloAndroid","Loaded " + url);
Log.i("HelloAndroid","Icon " + (view.getFavicon() == null ? "not" : "") + " found");
}
});
WebIconDatabase wid = WebIconDatabase.getInstance();
wid.requestIconForPageUrl(url, new WebIconDatabase.IconListener() {
public void onReceivedIcon(String url, Bitmap icon) {
Log.i("HelloAndroid","Found Icon for " + url);
}
});
wv.loadUrl("http://stackoverflow.com");
Log.i("HelloAndroid","Loading " + url);
The problem may be down to the fact that I'm not adding the WebView to a visible View. If you do get this to work I'd be interested to hear what you did.
So sorry for giving two half complete answers, but I thought it was worth posting what I found.
No accepted answer util now, I will share my findings here.
Windows .ico file format is a little complicated, it might contains one or more small images at multiple sizes and color depths, such that they may be scaled appropriately. Refer ICO_(file_format)
So when using unix "file" command to check the icon file type, you might get the following result:
a.ico : MS Windows icon resource - 1 icon, 32x32
b.ico : MS Windows icon resource - 9 icons, 256x256
Note a.ico and b.ico contains different number of icons.
I tried to use BitmapFactory.decodeFile to decode these icons.
Two Android devices with Android 4.3 can only decode a.ico, but can not decode b.ico.
Devices with Android 4.4 or later can decode both a.ico and b.ico.
As I only have limited Android devices, I can't give any conclusion. Maybe anyone else could help.
So if you really want to decode .ico files, you may try:
Create .ico files with only 1 image/picture in it
Write your own .ico decoder or 3rd library like image4j
I was also stuck with this since Picasso didn't seem to parse .ico files.
Surprisingly however, the solution is fairly simple.
InputStream in = new java.net.URL("https://" + domain + "/favicon.ico").openStream();
Bitmap b = BitmapFactory.decodeStream(in);
For me, this solution works with most of the websites but produces an empty bitmap whenever the size of the icon file is exceptionally low.
Related
In my app I am able to pick up a picture from the Gallery and show it on my phone. The picture has extension JPG. But when I email it to myself, save it on the server and then try to display it on my phone, it does not display. I even tried to downsize it to 30% using my email app on the phone, so now it is 220KB instead of 1.4MB but it still does not display.
In both cases I use the method
imageView.setImageBitmap(BitmapFactory.decodeFile(personPicture))
What do I need to do to overcome this problem?
BTW: the name of the picture was changed when I saved it on the server. I do not think it matters but I mentioned it anyway.
EDIT
The above is all the code I am using. Just to complete the issue here is the code that handles both jpg and png and it works if the picture is renamed to png.
if (url.contains("jpg")) {
imageView.setImageBitmap(BitmapFactory.decodeFile(url));
} else {
Drawable drw = LoadImageFromWebOperations(url);
if (drw != null) {
imageView.setImageDrawable(drw);
}
}
Note: the 1.4MB PNG file worked fine on the emulator but gave Out of memory exception on the device. When I re-sized the PNG file to 350KB it displayed properly on the device also.
If needed here is the url used in the above code (a picture of a cat)/
http://212.150.56.58:8080/feedback/pictures/56.png
When you try to load image from server into app, load it using Picasso library like as below:
Picasso.with(MainActivity.this).load("image_to_be_loaded").into(profile_image);
Edit
If you don't want to use third party library then try the following code:
public Drawable loadImageFromURL(String url, String name) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, name);
return d;
} catch (Exception e) {
return null;
}
}
Use Picasso to load server images to your app image view it also supports caching and lot many features.
Use is like this
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
checkout Picasso Library for more details.
I do not understand why, but if I renamed the picture on my server to extension PNG the picture displays, in all sizes.
I want to generate a PDF File from a View using the PdfDocument android class introduced in KitKat. I managed to do it, and the file is so far generated ok, ending up having a correct PDF. The only problem is the file is huge, 12Mb for just one page. Is there a way to reduce the File size?
The code I am using to generate the PDF is:
public static File generateDocument(Activity activity, String fileName, ViewGroup container) throws IOException{
File f = new File(activity.getExternalFilesDir(null), fileName);
PdfDocument document = new PdfDocument();
try{
for(int i=0;i<container.getChildCount();i++){
View v = container.getChildAt(i);
PdfDocument.PageInfo.Builder pageBuilder = new PdfDocument.PageInfo.Builder(v.getWidth(), v.getHeight(), i);
Page page = document.startPage(pageBuilder.create());
v.draw(page.getCanvas());
document.finishPage(page);
}
document.writeTo(new FileOutputStream(f));
} finally{
if(document!=null){
document.close();
}
}
return f;
}
In case anyone is still looking for a solution... I was working on a project to generate PDF from images and not satisfied with the file size generated by both Android's PdfDocument and 3rd party AndroidPdfWriter APW.
After some trials I ended up using Apache's PdfBox, which gave me a PDF file (A4 size with a single 1960x1080 image) for around 80K, while it's usually 2~3M with PdfDocument or AndroidPdfWriter.
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
// Define a content stream for adding to the PDF
contentStream = new PDPageContentStream(document, page);
Bitmap bimap = _get_your_bitmap_();
// Here you have great control of the compression rate and DPI on your image.
// Update 2017/11/22: The DPI param actually is useless as of current version v1.8.9.1 if you take a look into the source code. Compression rate is enough to achieve a much smaller file size.
PDImageXObject ximage = JPEGFactory.createFromImage(document, bitmap, 0.75, 72);
// You may want to call PDPage.getCropBox() in order to place your image
// somewhere inside this page rect with (x, y) and (width, height).
contentStream.drawImage(ximage, 0, 0);
// Make sure that the content stream is closed:
contentStream.close();
document.save(_your_file_path_);
document.close();
=====
btw. I guess the reason why they generate a huge file size is because they don't compress the image data while writing to PDF file. If you take a look into AndroidPdfWriter's XObjectImage.deflateImageData() method you will see it's using java.util.zip.Deflater.NO_COMPRESSION option to write the image data which is kind of horrible if you've got a picture with size 1960x1080. If you change the options to e.g. Deflater.BEST_COMPRESSION you get much smaller file size however it takes up to 3-4 seconds for me to handle one single page which is not acceptable.
There are a few main things that increases the size of a PDF file:
hi-resolution pictures (where lo-res would suffice)
embedded fonts (where content would still be readable "good enough" without them)
PDF content not required any more for the current version/view (older version of certain objects)
embedded ICC profiles
embedded third-party files (using the PDF as a container)
embedded job tickets (for printing)
embedded Javascript
and a few more
Try using iText. Following links give a basice idea for iText in android.
http://technotransit.wordpress.com/2011/06/17/using-itext-in-android/
http://www.mysamplecode.com/2013/05/android-itext-pdf-bluetooth-printer.html
https://stackoverflow.com/a/21025162/3110609
Using PDFDocument, be sure to downscale your images prior to drawing them in the canvas.
When drawing to the screen, this is enough to scale the bitmap :
canvas.drawBitmap(bmp, src, dst, paint);
However, when using the canvas from PdfDocument.Page.getCanvas, this canvas will not downscale the bitmap, it will just squeeze it into a smaller zone. Instead you should do something like this:
// Scale bitmap : filter = false since we are always downSampling
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, dstWidth, dstHeight,
false); // filter=false if downscaling, true if upscaling
canvas.drawBitmap(scaledBitmap, null, dst, paint);
scaledBitmap.recycle();
This is embedded in Android so it is much easier than using a third-party library. (The above was tested on a Marshmallow platform)
This seems to just be a bug in PdfDocument. The PDF file I created with PdfDocument was 5.6 megabytes. The same document generated through the iOS equivalent was 500K. If I take the Android PDF and run it through Adobe Acrobat's pdf optimization, without compressing any images, the 5.6MB file becomes 350K. They look identical, and I applied no compression in Adobe Acrobat.
In the actual PDF code, the Android image object dictionary is this
<</Type /XObject
/Subtype /Image
/Width 1224
/Height 1584
/ColorSpace /DeviceRGB
/BitsPerComponent 8
/Length 5816448
>>
The PDF from iOS has this dict
<< /Length 8 0 R
/Type /XObject
/Subtype /Image
/Width 1224
/Height 1584
/ColorSpace /DeviceRGB
/SMask 9 0 R
/BitsPerComponent 8
/Filter /FlateDecode >>
I think the problem is the lack of the FlateDecode filter in the Android version. When I run it through the Adobe Acrobat PDF optimizer, it gets the FlateDecode filter.
Is there any way to get a high resolution screen shot of a certain view in an activity.
I want to convert html content of my webview to PDF. For that I tried to take screen shot of the webview content and then converted it to PDF using itext. The resulted PDF is not in much more clarity.
My code:
protected void takeimg() {
Picture picture = mWebView.capturePicture();
Bitmap b = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);
// byte[] bt = b.getNinePatchChunk();
// Bitmap b;
// View v1 = mWebView.getRootView();
// v1.setDrawingCacheEnabled(true);
// b = Bitmap.createBitmap(v1.getDrawingCache());
// v1.setDrawingCacheEnabled(false);
FileOutputStream fos = null;
try {
File root = new File(Environment.getExternalStorageDirectory(),
"Sample");
if (!root.exists()) {
root.mkdir();
}
String sdcardhtmlpath = root.getPath().toString() + "/"
+ "temp_1.png";
fos = new FileOutputStream(sdcardhtmlpath);
// fos = openFileOutput("samsp_1.jpg", MODE_WORLD_WRITEABLE);
if (fos != null) {
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
// fos.write(bt);
fos.close();
}
} catch (Exception e) {
Log.e("takeimg", e.toString());
e.printStackTrace();
}
}
protected void pdfimg() {
Document mydoc = new Document(PageSize.A3);
try {
File root = new File(Environment.getExternalStorageDirectory(),
"Sample");
if (!root.exists()) {
root.mkdir();
}
String sdcardhtmlpath = root.getPath().toString() + "/";
mydoc.setMargins(0, 0, 0, 0);
PdfWriter.getInstance(mydoc, new FileOutputStream(sdcardhtmlpath
+ PDFfilename));
mydoc.open();
Image image1 = Image.getInstance(sdcardhtmlpath + "temp_1.jpg");
image1.scalePercent(95f);
mydoc.add(image1);
// mydoc.newPage();
mydoc.close();
} catch (Exception e) {
Log.e("pdi name", e.toString());
}
}
Update: See Edit 3 for an answer to op's original question
There are two options:
Use a library to convert the HTML to PDF. This is by far the best option, since it will (probably) preserve text as vectors.
Get a high resolution render of the HTML and save it as a PNG (not PDF surely!).
For HTML to PDF, wkhtmltopdf looks like a good option, but it relies on Qt which you can't really use on Android. There are some other libraries but I doubt they do the PDF rendering very well.
For getting a high-res webview, you could try creating your own WebView and calling onMeasure(...) and onLayout(...) and pass appropriate parameters so the view is really big. Then call onDraw(myOwnCanvas) and the webview will draw itself to your canvas, which can be backed by a Bitmap using Canvas.setBitmap().
You can probably copy the state into the new WebView using something like
screenshotterWebview.onRestoreInstanceState(mWebView.onSaveInstanceState());
Orrr it may even be possible to use the same WebView, just temporarily resize it to be large, onDraw() it to your canvas, and resize it back again. That's getting very hacky though!
You might run into memory issues if you make it too big.
Edit 1
I thought of a third, exactly-what-you-want option, but it's kind of hardcore. You can create a custom Canvas, that writes to a PDF. In fact, it is almost easy, because underlying Canvas is Skia, which actually includes a PDF backend. Unfortunately you don't get access to it on Android, so you'll basically have to build your own copy of it on Android (there are instructions), and duplicate/override all the Canvas methods to point to your Skia instead of Androids. Note that there is a tempting Picture.writeToStream() method which serializes the Skia data, but unfortunately this format is not forwards or backwards compatible so if you use it your code will probably only work on a few versions of Android.
I'll update if/when I have fully working code.
Edit 2
Actually it is impossible to make your own "intercepting" Canvas. I started doing it and went through the tedious process of serializing all function calls. A few you can't do because they are hidden, but those didn't look important. But right at the end I came to serializing Path only to discover that it is write-only. That seems like a killer to me, so the only option is to interpret the result of Picture.writeToStream(). Fortunately there are only two versions of that format in use, and they are nearly identical.
Edit 3 - Really simple way to get a high resolution Bitmap of a view
Ok, it turns out just getting a high res bitmap of a view (which can be the entire app) is trivial. Here is how to get double resolution. Obviously all the bitmaps look a bit crap, but the text is rendered at full resolution:
View window = activity.getWindow().getDecorView()
Canvas bitmapCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(window.getWidth()*2, window.getHeight()*2, Bitmap.Config.ARGB_8888);
bitmapCanvas.setBitmap(bitmap);
bitmapCanvas.scale(2.0f, 2.0f);
window.draw(bitmapCanvas);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, myOutputStream);
Works like a charm. I've now given up on getting a PDF screenshot with vector text. It's certainly possible, but very difficult. Instead I am working on getting a high-res PSD where each draw operation is a separate layer, which should be much easier.
Edit 4
Woa this is getting a bit long, but success! I've generated an .xcf (GIMP) and PDF where each layer is a different canvas drawing operation. It's not quite as fine-grained as I was expecting, but still, pretty useful!
Actually my code just outputs full-size PNGs and I used "Open as layers..." and "Autocrop layer" in GIMP to make these files, but of course you can do that in code if you like. I think I will turn this into a blog post.
Download the GIMP or Photoshop demo file (rendered at 3x resolution).
When you capture the view, just screen bound will capture ( due to control weight and android render pipeline ).
Capturing screenshot for converting to PDF is tricky way. I think two way is more reasonable solutions.
Solution #1
Write a parser ( it's simple ) to convert webview content ( that is HTML ) to iText format.
You can refer to this article for more information.
http://www.vogella.com/articles/JavaPDF/article.html
Also to write a parser you can use REGEX and provide your own methods like parseTable, parseImage, ...
Solution #2 Internet Required
Provide a URL ( or webservice ) to convert HTML to PDF using PHP or C# that has a lot of nice libraries. Next you can send download link to the Client ( Android Device ).
So you can also dynamically add some Tags, Banners, ... to the PDF from server side.
Screen Shot is nothing but picture of your device display which usually depend upon your phone absolute pixels, if your phone is 480x800 screen shot will be same and generally applicable for all scenarios.
Sure, Use this:
Bitmap bitmap;
View v1 = MyView.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
Here MyView is the View you need a screenshot of.
Having kind of an issue with initiating 9patch drawables from input streams. I need to skin my app and need to download skin elements and images from a web service.
Sought through a reasonable amount of resources both in SO and android dev guides, but none seem to work for me.
Setting a drawable from a resource does handle 9patch properly so logically the smarts to do so is there, but for some reason the following code, which I derived from the android sources itself, fails to handle 9patch properly
Rect pad = new Rect();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScreenDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bm = BitmapFactory.decodeResourceStream(resources, typedValue, new FileInputStream(path), pad, opts);
if (bm != null) {
byte[] np = bm.getNinePatchChunk();
if (np == null || !NinePatch.isNinePatchChunk(np)) {
np = null;
pad = null;
}
if (np != null) {
drawable = new NinePatchDrawable(resources, bm, np, pad, path);
} else {
drawable = new BitmapDrawable(resources, bm);
}
}
I have changed the input source to be the one of my files (FileInputStream(path)), in the android sources it is the input source initiated on resource images.
This code always returns BitmapDrawable even if the input image is a 9-patch.
Does anyone succeed actually getting this functionality working?
I'd appreciate any help or hint towards a solution.
Thank you in advance.
Okay, the solution is that there's no solution here, because 9 patch requires nine patch chunk as an array which is being generated at compile time. Obviously we do not have a compile phase when loading images from a web resource.
To Android engineers - maybe future release of android SDKs will be able to generate the nine patch chunk at run time.
I've created this gist to create 9patches at runtime: https://gist.github.com/4391807
I've downloaded a webpage html and the images inside it. Now, i'm trying to display them to the user. I've tried two different methods, and not sure which is best to use. Both have their issues.
First, I tried a text view with the following code:
TextView content = (TextView)findViewById(R.id.article_content);
MyImageGetter mig = new MyImageGetter(this, urlId);
Spanned span = Html.fromHtml(contents, mig, null);
content.setText(span);
I really like how this works, except two issues. The first, and most difficult is when an article has lots of images, I get OutOfMemory fc's. MyImageGetter code is as follows
public class MyImageGetter implements Html.ImageGetter{
String urlId = null;
Context c = null;
public MyImageGetter(ArticleViewer articleViewer, String urlId2) {
c = articleViewer;
urlId = urlId2;
}
public Drawable getDrawable(String source) {
String[] brokenUrl = source.split("/");
String imgName = brokenUrl[brokenUrl.length-1];
File image = new File("/data/data/com.that1dev.RibbonReader/Offline/" + urlId + "/" + imgName);
Log.w("MyApp", image.getAbsolutePath());
Bitmap bm = BitmapFactory.decodeFile(image.getAbsolutePath());
Drawable d = new BitmapDrawable(c.getResources(), bm);
d.setBounds(0,0, d.getIntrinsicWidth(),d.getIntrinsicHeight());
return d;
}
}
The other issue is the textview has different widths based on user choice and device orientation. The images in the view simply get cropped if they are larger than the textview width. However, I believe I can fix that without too much difficultly on my own. I just haven't tried too hard yet since I'm trying to fix the memory issue first. However, any assistance on either would be much appreciated.
The other method I've tried is a webview.
WebView webContent = (WebView)findViewById(R.id.web_content);
webContent.loadDataWithBaseURL("", contents[1], "text/html", "utf-8", "");
webContent.setBackgroundColor(Color.TRANSPARENT);
Unfortunately with this, the background stays white no matter what I try, and is incredibly ugly. I cannot seem to find a work around that works in 3.0 and 4.0.
If I had a choice, I'd really like the TextView method to work, since I preferred the look of it to the way the WebView rendered things.
What you're trying to do here, fundamentally, is change how the web content is rendered - swapping out what the website writer (which might be you, I don't know) wrote the background to be. Anyway HTML doesn't really support transparent backgrounds of the web content, so the only thing I can think of that you might try is to actually edit the web content via JavaScript:
myWebView.loadUrl("javascript:document.body.style.backgroundImage=\'\');
document.body.style.backgroundColor=\"#00FF00\");");
(Replace the above color with the color of your choice) calling that after the WebView loads will clear any background on the HTML, but you'll still have issues when it comes to any nested styling not on the body.
As for your image problem, you're opening all of the images at their default size and keeping them in memory. One of the things that the WebView does for you is to keep decimated (as in shrunk) renderings of the webpage images. If you want to fix your memory footprint, your best bet is to temporarily save the images to disk, and only open them when the user has scrolled to where the image needs to be - which is not going to be easy, by any means, but that's the only way to ensure that you aren't going to overflow your allocated heap space.