BitmapFactory fails to decode InputStream for my ImageVIew - java

Now I'm trying download an image from URL to my ImageView.
public class ImageRequestTask extends AsyncTask<String, Void, Bitmap> {
private final ImageView imageView;
public ImageRequestTask(ImageView imageView) {
this.imageView = imageView;
}
#Override
protected Bitmap doInBackground(String... sources) {
try {
InputStream is = new URL(sources[0]).openStream();
Bitmap img = BitmapFactory.decodeStream(is);
return img;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
}
But I get this message in logs:
D/skia: --- Failed to create image decoder with message 'unimplemented'
And img variable was null.
Tried to use Drawable.createFromStream(), but got the same issue.
Where it come from and what should I do?

Related

How to do tasks like HttpURLConnection in the background of a app using Java in Android Studio?

I am basically following a tutorial for learing Android Studio that uses AsyncTask class for doing background tasks like HttpURLConnection. But the thing is that it created a lot of error and later I found out that class was deprecated. So what is the alternative?
(Please provide an example code if you can)
CODE:
public class MainActivity extends AppCompatActivity {
public class DownloadingTask extends AsyncTask<String, Void, String>
{
#Override
protected String doInBackground(String... urls)
{
String result="";
HttpURLConnection urlConnection=null;
URL url;
try{
url=new URL(urls[0]);
urlConnection=(HttpURLConnection)url.openConnection();
InputStream in;
in = urlConnection.getInputStream();
InputStreamReader reader=new InputStreamReader(in);
int data=reader.read();
while (data!=-1)
{
char current=(char) data;
result = result + current;
data=reader.read();
}
return result;
}
catch (Exception e)
{
e.printStackTrace();
return "Failed";
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadingTask task= new DownloadingTask();
String s=null;
try {
s = task.execute("http://www.android.com/").get();
}
catch (Exception e)
{
e.printStackTrace();
}
Log.i("Msg",s);
}
}
This is an example that shows how to download an image using AsynkTask.
private class DownLoadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
DownLoadImageTask(ImageView imageView) {
this.imageView = imageView;
}
protected Bitmap doInBackground(String... urls) {
String urlOfImage = urls[0];
Bitmap logo = null;
try {
InputStream is = new URL(urlOfImage).openStream();
logo = BitmapFactory.decodeStream(is);
} catch (Exception e) { // Catch the download exception
Log.v("download", e.getMessage());
}
return logo;
}
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
}
This is how to run it
new DownLoadImageTask(img).execute(imageUrl);
Any update is done in this method onPostExecute as it runs on the UI Thread.
Yes, You are right. AsynkTask is deprecated. You can use LoderManager and Java Executor
Or Volley, Retrofit libraries
For downloading images, You can use Picasso and Gilde Libraries.

How to get image from url and cache it

I'm trying to get image from a url and updating it to an Image view. But the images are not being cached and reload when I scroll down then up
I'm using the following AsynkTask for updating images to ImageView
public class GetImageTask extends AsyncTask<Void, Void, Bitmap> {
private String url;
private ImageView imageView = null;
private CircleImageView circleImageView = null;
public GetImageTask(String url, CircleImageView circleImageView) {
this.url = url;
this.circleImageView = circleImageView;
}
public GetImageTask(String url, ImageView imageView) {
this.url = url;
this.imageView = imageView;
}
#Override
protected Bitmap doInBackground(Void... voids) {
try {
URL urlConnection = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlConnection.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
return BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if(imageView != null)
imageView.setImageBitmap(result);
if (circleImageView != null)
circleImageView.setImageBitmap(result);
}
}
But this doesn't cache the images please suggest an edit to cache the images

how to change image file name in android studio?

I am downloading an image from URL and displaying on an imageview but the problem is if image name starts with a digit it can't be displayed.
private class DownloadFilesTask extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Bitmap result) {
//loading.dismiss();
String image_name = result.toString();
if( Character.isDigit(image_name.charAt(0))){
}
imageView.setImageBitmap(result);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
}
Since code provided is unclear, so this might help you.
Use Picasso or Universal Image Loader to load Image on ImageView
or save bitmap to sdCard and then load it
see https://stackoverflow.com/a/34629526/5057663

Dowloading Image using URL

I'm trying to download an image using a URL and a button in my app. When I'm running it on my phone, I,m not able to download the image. Can anyone please point out the problem with this. Thanks for the help in advance :)
This is my code.
public class MainActivity extends AppCompatActivity {
ImageView download;
public void downloadImage(View view){
DownloadImage image = new DownloadImage();
Bitmap result;
try {
result = image.execute("https://en.wikipedia.org/wiki/File:Bart_Simpson_200px.png").get();
download.setImageBitmap(result);
}
catch(Exception e)
{
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
download = (ImageView) findViewById(R.id.imageView);
}
public class DownloadImage extends AsyncTask<String, Void, Bitmap>{
#Override
protected Bitmap doInBackground(String... urls) {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.connect();
InputStream in = urlConnection.getInputStream();
Bitmap Image = BitmapFactory.decodeStream(in);
in.close();
return Image;
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
you can download image from url in two ways
1.you can user Glide library to load image from url look the below code it can help you in simple way
compile this library
implementation 'com.github.bumptech.glide:glide:4.6.1'
than load image like this
Glide.with(MainActivity.this)
.load(url)
.apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
.into(imageview);
2 .try this if you dont want to use third party library
new DownloadImage(imamgeview).execute(url);
create a Async Task
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = (ImageView ) bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.d("Error", e.getStackTrace().toString());
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
i hope that it will work in your case
You did not call the function
downloadImage(view);
In the oncreate (after the findviewbyid line)
Also check if the internet permision is in the Android Manifest file :
<uses-permission android:name="android.permission.INTERNET" />
Having said that you should use a library like Glide\Picasso\Ion etc... much better than asynctask for this purpuse
https://github.com/bumptech/glide - Glide
https://github.com/koush/ion - Ion
http://square.github.io/picasso/ - picasso
Hope it helped.

Download image from Mysql with url to custom listview

I want to make Custom Listview on android. I downloaded one image from Mysql but ı Can Not downloaded second image in one post. The error is : E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3.
My downloader codes ;
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
I calling with this code ;
new DownloadImageTask((ImageView)itemView.findViewById(R.id.imageView)).execute(photo);
I suggest you to use Glide instead if you have the option, you will avoid the headache of synchronization and can easily handle the errors:
add this to your dependencies:
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
and then you can load the images with:
Glide.with(this).load(url).into(imageView);
source:
https://github.com/bumptech/glide

Categories

Resources