Here I want to convert an image from String URL. Though there is an URL containing image, it returns null. I have shared code below.
private byte[] convertImageToByteArray(String imgPath)
{
byte[] byteArray = null;
Bitmap bmp = BitmapFactory.decodeFile(imgPath);
if(bmp != null)
{
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
try
{
stream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
else
{
try {
Bitmap bmpDefault = BitmapFactory.decodeResource(getResources(), R.drawable.na);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//bmpDefault.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bmpDefault.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return byteArray;
}
Instead of executing if block, the control flow enters into else block and BitmapFactory.decodeFile() always returns null. Where I went wrong?
Ravindra's answer is helpful, for better utilization of image try Picasso lib,is mind blowing. It has also resize/crop method.
Picasso.with(getContext()).load("your url").into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
//do what ever you want with your bitmap
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
You can use this reference, it might be helpfull to you.
Note :- This function makes Network Connection, you should call it inside thread or AsyncTask. Otherwise it might throw NetworkOnMainThread
Exception.
As the function is returning Bitmap, you will have to wait till your thread is executed so check this question. which uses join()
I hope this helps.
Use these lines of code for it:-
Bitmap bmImg;
AsyncTask<String, String, String> _Task = new AsyncTask<String, String, String>()
{
@Override
protected void onPreExecute()
{
String _imgURL = "**HERE IS YOUR URL OF THE IMAGE**";
}
@Override
protected String doInBackground(String... arg0)
{
HttpGet httpRequest = new HttpGet(URI.create(_imgURL));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
bmImg = BitmapFactory.decodeStream(bufHttpEntity.getContent());
System.out.println("main url"+mainUrl);
httpRequest.abort();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result)
{
try
{
/////HERE USE YOURS BITMAP
**bmImg** is your bitmap , you can use it any where i your class
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
_Task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With