Is there a way to lazyload an image from the internet into a remoteview.
remoteView.setImageViewBitmap(R.id.image, UrlUtils.loadBitmap(bitmapUrl));
I use this function but it is blocking my widget during a small time.
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(getPageInputStream(url));
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream);
Utils.copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
Thanks
Absolutely
Draw your widget the usual way, all the textual parts, etc. beside image
Create a service which will load image. Here's good tutorial that includes how to create and call service from the appwidget
After updating your widget call the service. Pass widget ID and image URL
Load image from cache or remotely in your service and update your widget again. Voila, you have it now
Try android-query lib for lazyload loading.
https://code.google.com/p/android-query/#Image_Loading
Related
I wrote a little piece of code that download image from internet and cache them into cache dir.
It runs in a secondary thread.
{
String hash = md5(urlString);
File f = new File(m_cacheDir, hash);
if (f.exists())
{
Drawable d = Drawable.createFromPath(f.getAbsolutePath());
return d;
}
try {
InputStream is = download(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
if (drawable != null)
{
FileOutputStream out = new FileOutputStream(f);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
bitmap.compress(CompressFormat.JPEG, 90, out);
}
return drawable;
} catch (Throwable e) { }
return null;
}
I use this code to load picture inside a ListView item, and it works fine. If I remove the first if (where i load image from disk) it runs smoothly (and download picture every time!). If I keep it, when you scroll listview you feel some lags during picture's loading from disk, why?
To answer the question "why", I experienced this with lots of gc() messages in my logcat. Android allocates the memory before decoding the file from disk which could cause garbage collection, which is painful for the performance in all threads. Probably the same happens when you encode your jpeg as well.
For decoding part you can try reuse existing bitmap if you have one to let Android decode image in-place. Please have a look at the following snippet:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
options.inMutable = true;
if (oldBitmap != null) {
options.inBitmap = oldBitmap;
}
return BitmapFactory.decodeFile(f.getAbsolutePath(), options);
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
Do in background.( use AsyncTask to load images.)
I am trying to use the VuDroid PDF viewer and I need to take the rendered bitmap and store it as a byte[]. Then I need to convert it back into a Bitmap that can be displayed on a view using something like "canvas.drawBitmap(bitmap, 0, 0, paint);".
I have spent many hours trying to access the Bitmap and I might have done it already, but even if I get the byte[] to return something it still wont render as a Bitmap on the canvas.
Could someone please help me here, I must be missing something. Thank you so much.
I believe it is supposed to accessed via...
PDFPage.java .... public Bitmap renderBitmap(int width, int height, RectF pageSliceBounds)
-or-
through Page.java -or- DocumentView.java -or- DecodeService.java
Like I said I have tried all of these and have gotten results I just cannot see where I am going wrong since I cannot render it to see if the Bitmap was called correctly.
Thank you again :)
The doc says the method returns "null if the image could not be decode." You can try:
byte[] image = services.getImageBuffer(1024, 600);
InputStream is = new ByteArrayInputStream(image);
Bitmap bmp = BitmapFactory.decodeStream(is);
I think This will help you:-
Render a byte[] as Bitmap in Android
How does Bitmap.Save(Stream, ImageFormat) format the data?
Copy image with alpha channel to clipboard with custom background color?
if you want to get each pdf page as independent bitmap you should consider that
VuDroid render the pages,
PDFView only display them.
you should use VuDroid functions.
now you can use this example and create your own codes
Example code : for make bitmap from a specific PDF page
view = (ImageView)findViewById(R.id.imageView1);
pdf_conext = new PdfContext();
PdfDocument d = pdf_conext.openDocument(Environment.getExternalStorageDirectory() + "your PDF path");
PdfPage vuPage = d.getPage(1); // choose your page number
RectF rf = new RectF();
rf.bottom = rf.right = (float)1.0;
Bitmap bitmap = vuPage.renderBitmap(60, 60, rf); //define width and height of bitmap
view.setImageBitmap(bitmap);
for writing this bitmap on SDCARD :
try {
File mediaImage = new File(Environment.getExternalStorageDirectory().toString() + "your path for save thumbnail images ");
FileOutputStream out = new FileOutputStream(mediaImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
for retrieve saved image:
File file = new File(Environment.getExternalStorageDirectory().toString()+ "your path for save thumbnail images ");
String path = file.getAbsolutePath();
if (path != null){
view = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), YOUR_X, YOUR_Y, false);
}
Try this code to check whether bitmap is properly generating or not
PdfContext pdf_conext = new PdfContext();
PdfDocument d = (PdfDocument) pdf_conext.openDocument(pdfPath);
PdfPage vuPage = (PdfPage) d.getPage(0);
RectF rf = new RectF();
Bitmap bitmap = vuPage.renderBitmap(1000,600, rf);
File dir1 = new File (root.getAbsolutePath() + "/IMAGES");
dir1.mkdirs();
String fname = "Image-"+ 2 +".jpg";
File file = new File (dir1, fname);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
I saved my bitmap images in my internal storage but i can't redisplay it. I've been researching for a long time but i've not find yet.
public static void saveImages(Activity activity) throws IOException
{
for (int i=0; i<categories.getItems().length; i++) {
OutputStream os2 = activity.openFileOutput(categories.getItems()[i].getName(),
Context.MODE_WORLD_READABLE);
OutputStreamWriter osw2 = new OutputStreamWriter(os2);
Bitmap bmp = ((BitmapDrawable)categories.getItems()[i].getCategoryImage()).getBitmap();
bmp.compress(Bitmap.CompressFormat.PNG, 90, os2);
osw2.close();
}
}
This code works succesfully to save images. I will redisplay that images from files.
Thank you
Try this code: uses openFileInput to fetch the streams you saved and then decodes them:
for (int i=0; i<categories.getItems().length; i++) {
InputStream is = activity.openFileInput(categories.getItems()[i].getName());
Bitmap b = BitmapFactory.decodeStream(is);
// do whatever you need with b
}
Try this
File f=new File(yourdir, imagename);
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
decode bitmap, and then make a new imageView then add the bitmap to the imageView.
I am trying to open a bitmap that has already been stored in SdCard as follows:
String imageFilePath= "/sdcard/SoftCopy/"+mybitmap.png;
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
Bitmap loadedWork= BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
I have a second Bitmap named currentWork. This bitmap is actually the current drawing that has been done. I have combined two bitmaps as follows:
Canvas c = new Canvas(loadedWork);
c.drawBitmap(currentWork, 0, 0, null); //so that currentWork get drawn on loadedWork
Now i am saving the combined bitmap (now in loadedWork) to file as follows:
try {
final FileOutputStream out = new FileOutputStream(new File("/sdcard/SoftCopy" + "/mybitmap.png"));
loadedWork.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
return true;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
THE problem is that the combined bitmap(loadedWork) gets saved as png file for 1st time and i am able to load it, however WHEN I AGAIN TRY TO SAVE AFTER MAKING SOME MODIFICATIOns, then the application crashes. Can someone tell me how can I be able to resave the combined bitmap.
I am trying to download images from a remote server, the number of images downloaded is 30. The code i am using to download image is as below. Some images download successfully and some images don't download and raises the above exception. What might be the problem.
public static Bitmap loadBitmap(String url)
{
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), 4*1024);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, 4 * 1024);
int byte_;
while ((byte_ = in.read()) != -1)
out.write(byte_);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e("","Could not load Bitmap from: " + url);
} finally {
try{
in.close();
out.close();
}catch( IOException e )
{
System.out.println(e);
}
}
return bitmap;
}
Please look on my this post
Image download code works for all image format, issues with PNG format rendering
In my case I solved this error with encode the url
Because image url that i wanted to download has the Persian letters(or other Unicode character) in it
So I replaced all Persian characters with encoded UTF-8 letters