I have URL to PNG image file.
I want to get this image file and set it as a source for an ImageView.
My code:
URL iconURL = null;
try {
iconURL = new URL("https://maps.gstatic.com/mapfiles/place_api/icons/worship_general-71.png");
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap icon = null;
try {
icon = BitmapFactory.decodeStream(iconURL.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
myImageView.setImageBitmap(icon);
If I place a breakpoint into the line with decodeStream() the app stops and I see the following:
If I run the app in regular mode, it just stops!
Can someone explain what is wrong here and how to do it correctly?
Thanks.
If you just want to show the image on your ImageView then you can use Picasso Library.
Put below dependency in build.gradle file
'com.squareup.picasso:picasso:2.71828'
And use below java code for loading your image from URL to your ImageView
Picasso.get()
.load(url)
.into(imageView);
Related
I'm trying to make preview as Thumbnail from images on web before downloading, it means i have more item on RecyclerView and i'm trying to view simple blurred preview from images but i dont know whats solution, i use this below code but i think this code download full image, like with social application such as whatsApp or Telegram how can i make preview image on web and show that to users?
new Thread(new Runnable() {
#Override
public void run() {
//First create a new URL object
URL url = null;
try {
if (checkNetWorkConnection()) {
url = new URL("http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-2.jpeg");
//Next create a file, the example below will save to the SDCARD using JPEG format
File file = new File(APP.DIR_APP + APP.IMAGE + "/" + "Wallpaper-2.jpg");
//Next create a Bitmap object and download the image to bitmap
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
//Finally compress the bitmap, saving to the file previously created
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, new FileOutputStream(file));
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e("Error ", e.getMessage());
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("Error ", e.getMessage());
} catch (IOException e) {
e.printStackTrace();
Log.e("Error ", e.getMessage());
}
}
}).start();
You can use picasso lib.
Picasso
.with(context)
.load("http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-2.jpeg")
.resize(600, 200)
.centerInside()
.into(imageViewResizeCenterInside);
You can give circle shape to image using Transformation api of picasso.
refer this Picasso
You can try implement with this zoom effect. It is being use by many application. With the example you can create your own background like full screen black background but you need to implement it yourself which is pretty simple but kinda lot of work to do.
Beside why not try to use Glide or Picasso to download and display the image? It help you save a lot work such as caching, download in asyncs,resize etc.
When you work with image especially with recycleview or listview there is some problem you need to always remember. Every time you scroll the listView/RecycleView it actually re-download the image so you have to really check wether it already exist in your sdcard to avoid this problem beside there is better to cache is memory although you already save it on sd card but still you can avoid memory crash by cache it in memory. Those hard work done by using either one of the library I just show you.
I know how to read the gif image in local package assets directory. But now I need read other gif image in other package assets. for example the main APP com.aaa.app to read the com.bbb.app gif in assets. I know use
try {
context = this.createPackageContext("com.bbb.app",
Context.CONTEXT_IGNORE_SECURITY);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
AssetManager am = context.getResources().getAssets();
try {
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
to read jpg/png, but I want to read gif use Fresco lib. Anyone know how to do it?
once you have the uri of the gif, do this to load it with fresco
SimpleDraweeView simpleDraweeView = (SimpleDraweeView) findViewById(R.id.imageview);
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithResourceId(R.raw.sample_gif).build();
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(imageRequest.getSourceUri())
.setAutoPlayAnimations(true)
.build();
simpleDraweeView.setController(controller);
Don't forget to add this to your build.gradle:
compile 'com.facebook.fresco:animated-gif:0.12.0'
At the moment, Fresco supports the following:
Uri uri = Uri.parse("asset:///image.png");
Which gets translated into:
AssetManager.openFd("image.png");
I've created a folder 'test' inside res and I want to display them in an image view. How exactly can I fetch the image with a given name in the designated folder?
ImageView test = (ImageView) testing.findViewById(R.id.test);
flag.setImageDrawable(getResources().); <==== This?
EDIT
InputStream is = null;
try {
is = this.getResources().getAssets().open("country_flags/sample.png");
} catch (IOException e) {
;
}
image = (ImageDrawable) BitmapFactory.decodeStream(is);
try {
flag.setImageDrawable(getResources().getAssets().open("country_flags/"+nationality+".png"));
} catch (IOException e) {
e.printStackTrace();
}
How exactly can I fetch the image with a given name in the designated folder?
You don't. You cannot invent new resource types, and so your test directory will, at best, be forever ignored.
Am trying to download an image using jsoup library and set it to image view but the downloaded image is cropped automatically. i don't know if it is the library or something else. Here is my code, Any one to help me please?
try {
// Connect to the web site
Document documentImage1 = Jsoup.connect(url).get();
// Using Elements to get the class data
Element img = documentImage1.select("div[class=media-img imgflare--river] img[src]").first();
// Locate the src attribute
String imgSrcImage1 = img.attr("src");
// Download image from URL
InputStream inputImage1 = new java.net.URL(imgSrcImage1).openStream();
// Decode Bitmap
image1 = BitmapFactory.decodeStream(inputImage1);
} catch (IOException e) {
e.printStackTrace();
}
I want to use the image directly from website. I did not place that image in drawable folder. For this purpose what can I do?
Please, give me some suggestions.
I've written a function that creates a Drawable from an image url, look below:
private Drawable downloadThumbnails(String url) {
Drawable d = null;
try {
InputStream is = (InputStream) this.fetch(url);
d = Drawable.createFromStream(is, "src_from_stream");
return d;
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return null;
}
public Object fetch(String address) throws MalformedURLException, IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
then use it like this:
Drawable img = downloadThumbnails("http://yoururl.com/image.png");
yourImageView.setImageDrawable(img);
I strongly recommend using Picasso:
Picasso.with(context).load("http://an.url.com/lovely_cats.png").into(imageView);
It has a lot of usefull functions that allows you to:
crop the image
resize the image
rotate the image
fit an image into the target view
replace not downloaded file with local placeholder
an even more - mostly in a single line (or call).