Android: Widget load images - android

I am unable to fiund how to download images to my widget!
In my widget I load in AsyncTask json with url, title, then I show title in TextView and I need to load images from url.
I tried with this but image load, and now showing
class LoadImages extends AsyncTask<Void, Void, Bitmap>{
#Override
protected Bitmap doInBackground(Void... params) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL("http://mysite/simple.img").openStream());
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
Log.e("Log", "Yeah");
} catch (IOException e) {
Log.e("Log", "Could not load Bitmap from: " + "mysiteg");
}
return bitmap;
}
}
And on Update I call this
LoadImages load = new LoadImages();
load.execute();
Bitmap bitmap = load.get();
update.setImageViewBitmap(R.id.imageView0, bitmap);

You should override onPostExecute method in LoadImages class, and set bitmap to your ImageView in that method. When LoadImages task is done, it will call onPostExcute method. Considering WeakReference to wrap your ImageView for better performance.

Related

How to disable image from saving into internal memory while downloading from an url

I am using the following code to download an image from an url, then saving to sqlite and then view in imageview in an activity.
new LoadProfileImage().execute(jsonObject.getString("image"), id, title, promoexpdate, String.valueOf(i),flag,promostartDate);
The above code is used to call the function to do the above work.
private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
String x,y,z,a,w,s;
protected Bitmap doInBackground(String... uri) {
String url = uri[0];
Log.d("ImageURL",url);
x = uri[1];
y = uri[2];
z = uri[3];
a = uri[4];
w = uri[5];
s = uri[6];
Log.d("LogValue",url+x+y+z+a+w+s);
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(url).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (IOException e) {
Log.e("ErroronImageParsing", e.getLocalizedMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
if (result != null) {
int width = result.getWidth();
int height = result.getHeight();
Bitmap newBitmap = Bitmap.createScaledBitmap(result, width / 2, height / 2, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
buffer = out.toByteArray();
if (result!= newBitmap){
result.recycle();
}
Log.d("ImageUploaded", "Success");
}
try {
dbManager.open();
Cursor cursor = dbManager.fetch_PromsID(x);
if (cursor.getCount() > 0){
String fla = cursor.getString(cursor.getColumnIndex(DatabaseHelper.PRO_FLAG));
String pri_ID = cursor.getString(cursor.getColumnIndex(DatabaseHelper.PRO_ID));
if (!w.equals(fla)) {
dbManager.update_Promotions(pri_ID,y,z, buffer,w,s);
}
}else {
dbManager.insertPromotions(x,y,z,buffer,w,s);
}
} catch (SQLException e) {
e.printStackTrace();
}
SqliteData();
panel.setVisibility(View.GONE);
dbManager.close();
}
}
Here, when the code below is executed, image from the url is saved into internal storage. I wish to disable the auto saving while maintaining my intention. Thanks in advance...
Bitmap newBitmap = Bitmap.createScaledBitmap(result, width / 2, height / 2, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
buffer = out.toByteArray();
Try to use third library like Picasso or Glid
that offer
loading without caching
loading with memory or storage caching
you can do it with single line of code
Picasso.with(context).load(imageUrl)
.error(R.drawable.error)
.placeholder(R.drawable.placeholder)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageView);

How to convert a thumbnail from Box android-content-sdk to a bitmap?

As seen in this issue there are two methods in Box android-content-sdk that return a request:
How to get thumbnails with box android-content-sdk (notV2)
The target of the first is a local file and the other a OutputStram object.
In my case I would get a bitmap in a AsyncTask like this:
Bitmap bm = BitmapFactory.decodeStream(InputStream is)
So finding no solution with the Box android-sdk-content, I tried to convert the outputStream to InputStream like this:(This method is called in AsynckTask)
protected Bitmap getThumbBox(final String id, final BoxApiFile apiFile) throws IOException {
Bitmap bitmap = null;
PipedInputStream in = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(in);
new Thread(
new Runnable(){
public void run(){
//put your code that writes data to the outputstream here.
BoxRequestsFile.DownloadThumbnail downloadThumbnail = apiFile.getDownloadThumbnailRequest(out,id);
}
}
).start();
//data can be read from the pipedInputStream here.
bitmap = BitmapFactory.decodeStream(in);
return bitmap;
}
According with this:
http://io-tools.sourceforge.net/easystream/outputstream_to_inputstream/Pipes.html
But with no success.
Any ideas?
Thanks.
Here is how I managed to get the thumbnail into a bitmap:
protected Bitmap getThumbBox(final String id, final BoxSession session) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BoxDownload req = null;
try {
req = new BoxApiFile(session).getDownloadThumbnailRequest(bos, id).setMinSize(256).send();
} catch (BoxException e) {
e.printStackTrace();
}
byte[] byteArr = bos.toByteArray();
Bitmap bm = BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length);
return bm;
}

Android image view makes the app slow

In my app i have an ImageView. Before adding that ImageView the app was performing smooth. now it throws ANR.
The image is saved in the database as base 64 encode string and it is decoded to bitmap and loaded to the imageview using :
imageView.setImageBitmap(bitmap);
The conversion of bitmap and applying the bitmap to ImageView all those things are done in an AsyncTask:
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private String data = "";
public BitmapWorkerTask(ImageView imageView, String data) {
imageViewReference = new WeakReference<ImageView>(imageView);
this.data = data;
}
#Override
protected Bitmap doInBackground(Integer... params) {
byte[] decodedString = Base64.decode(data, Base64.DEFAULT);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inPurgeable = true;
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length, options);
return decodedByte;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
The AsyncTask is called from the main ui using the following code:
BitmapWorkerTask task = new BitmapWorkerTask(pollWebView,decodedStrings[1]);
task.execute();
decodedStrings[1] contains the base64 encoded image dataUrl.
Any solutions for this problem?
The major issue which is letting your app slow is WeakReference remove it and try whithout that
don't use this
private final WeakReference<ImageView> imageViewReference;
just use
private final ImageView imageViewReference;
Use following code to compress image (75% compression)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 75, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

How to create map as ImageView in android?

I am trying to create a ImageView with help of MapView something like in pic:
Guys please give me some idea how to do this.
If you want a static map, you can just do the same as me:
http://maps.google.com/maps/api/staticmap?center=-15.800513%2C-47.91378&zoom=16&format=png&maptype=roadmap&mobile=false&markers=|color:%23128DD9|label:Marker|-15.800513%2C-47.91378&size=1000x400&key=&sensor=false
Change the parameters
?center= which will tell you where the center of the image shoud
be
label:Marker The position where the marker should appear.
To load this image to an ImageView:
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
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);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
This method will return a Bitmap to set this Bitmap in the ImageView just do like this:
ImageView img = (ImageView)findViewById(R.id.imageView1);
Bitmap b = loadBitmap(urlToTheImage);
img.setImageBitmap(b);
I'm not exactly sure what you mean, but from the looks of it you want the Google Static Maps API.
https://developers.google.com/maps/documentation/staticmaps/
This will generate an image of a map when you give it the latitude, longitude etc. You can then use this in an ImageView as you require.
The advantage is that you don't have to create an expensive MapView, but it will not be interactive

Problem with downloading image from URL

I use such code for downloading image from URL:
public static Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
When I send URL "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png" it works fine, but when I use "http://www.hospimedica.com/images/stories/articles/article_images/_CC/20110328%20-%20DJB146.gif" it returns me null.
What's wrong with this URL?
Why are you writing your own method to downlaod an image ? Android has inbuilt method to achieve this.. Just use
URL url = new URL("Your url");
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

Categories

Resources