A very peculiar problem seen when loading an image from the following URL:
https://codechef_shared.s3.amazonaws.com/sites/default/files/uploads/landing_page_banners/small-banner-updatedcook51.png
However I tried loading images from other sources and they loaded successfully. And there is no error reported!
I have used both picasso and volley's network image view for this URL and both are unable to load!
Picasso.with(context).load("https://codechef_shared.s3.amazonaws.com/sites/default/files/uploads/landing_page_banners/small-banner-updatedcook51.png")
.resize(200, 200).centerCrop()
.into(viewHolder.ivEventImage);
Try downgrade image if is of large in size with the method :
ImageView myImgView = (ImageView) findViewById(R.id.imageView);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, newWidth, newHeight, true);
myImgView .setImageBitmap(bMapScaled);
Related
I have to set image in Image view. I used Glide library to set image in recycler view, now when I click to recycler view item I want to set it into another activity.
I just take image url through intent and store it as a string variable.
final String image = intent.getStringExtra("Image");
Then I tried to set image using the following line of code.
displayImage.setImageURI(Uri.parse(image));
Note: displayImage is my targeted imageview.
And I could get the correct url here.
Well when you have already set image once in RecyclerView why not use Bitmap from that ImageView. This will save network data and time.
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bs);
intent.putExtra("byteArray", bs.toByteArray());
startActivity(intent);
Then in target activity
if (getIntent().hasExtra("byteArray")) {
Bitmap bitmap =
BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length);
imageView.setImageBitmap(bitmap);
}
You can not display image from URL using setImageURI();
You need to pass url to your activity than display it using Glide
Use this
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_placeholder);
requestOptions.error(R.drawable.ic_error);
Glide.with(this)
.load(image)
.apply(requestOptions)
.into(displayImage);
Instead of this
displayImage.setImageURI(Uri.parse(image));
I need to take an image from imageView and make it as a bitmap.
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
Glide.with(this)
.load(s)
.into(mImageViewFilter);
Bitmap bitmap = ((BitmapDrawable)mImageViewFilter.getDrawable()).getBitmap();
You can see that into mImageViewFilter I am loading a photo with use of its URI and Glide library.
My next step is to take this photo which is in mImageViewFilter and make it as a bitmap. After the last line of code there is an exception. What am I doing wrong?
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
To load image into ImageView use below code
Glide.with(this).load(s).into(mImageViewFilter);
So in order to get bitmap from ImageView (mImageViewFilter )
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
OR
You can add this before the code where you are trying to get Bitmap out of the ImageView
mImageViewFilter.setDrawingCacheEnabled(true);
In order to get the Bitmap out of the ImageView using DrawingCache, you first need to enable ImageView to draw image cache.
Bitmap bitmap = mImageViewFilter.getDrawingCache();
Actually you doing right thing. But the problem is Image loading is Asynchronous call.
And you called getBitmap() in right next line which will be called sequentially.
And it will give you null anyway cause image is not loaded yet .
Solution
if you want to do it in next direct step .You can use direct Bitmap callback.
Glide.with(this)
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
// Use resource as bitmap save it for later.
}
});
First take it as bitmap, then take it again to apply to our ImageView :
Bitmap bitmap = Glide.with(this) //taking as bitmap
.load(s)
.asBitmap()
.into(100, 100) //width and height
.get();
Glide.with(this) //apply to imageview
.load(s)
.into(mImageViewFilter);
The straightforward way to obtain a bitmap from any View including ImageView is to get the drawing cache from it.
mImageViewFilter.buildDrawingCache();
Bitmap bitmap = mImageViewFilter.getDrawingCache();
But the above method may give you a low quality image.
I think you should directly decode the bitmap from the Uri using BitmapFactory
InputStream in = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();
I have a NetworkImageView where I download images from a server. However, the requirement is to have the ability to rotate the image after downloading the image. I have a rotate image function but I need to get the Bitmap associated to the NetworkImageView in order for me to rotate the bitmap. How can I access the BitMap from a NetworkImageView?
You can use below method to get bitmap of any view.
Bitmap bitmap = viewToBitmap(mImageView);
Bitmap bitmap = viewToBitmap(mLinearlayout);
Bitmap bitmap = viewToBitmap(mRelativelayout);
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Hope this helps
UPDATE
Create a volley image request.
After the image has been loaded and set inside the NetworkImageView this code will work. Else it will return a Null pointer since there is no view/image inside the NetworkImageView
mNetworkImageView = (NetworkImageView) view.findViewById(R.id.networkImage);
Bitmap bitmap = ((BitmapDrawable) mNetworkImageView.getDrawable()).getBitmap();
There is another way to use Volley and obtain the Bitmap.
Create an ImageRequest
inside the onResponse(), obtain the Bitmap and set it to your ImageView (Not NetworkImageView). I had mentioned this method in the comments above.
I am using the following code to get a Bitmap image of the html webpage that is loaded in WebView, however getting crashes due to running out of memory. how do i avoid memory problems if it is directly converting the picturedrawable into a Bitmap.
Is there a way to load file directly into a compressed format lke jpg? or is there some other way to handle this problem?
using Bitmapfactory to downsize the image does not look like it is possible, because the Bitmap is already created so it would be too late to do anything about it.
wv = (WebView)findViewById(R.id.webView1);
wv.loadUrl("http://www.google.com");
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
Picture picture = wv.capturePicture();
HTMLBitmap = pictureDrawable2Bitmap(picture);
Toast.makeText(HTMLActivity.this, "bitmap " + HTMLBitmap.toString(), Toast.LENGTH_LONG).show();
Bitmap bmp = Bitmap.createScaledBitmap(HTMLBitmap, 500, 300, false);
// the imageView that is being set to the bitmap
imageOne.setImageBitmap(bmp);
private static Bitmap pictureDrawable2Bitmap(Picture picture){
PictureDrawable pictureDrawable = new PictureDrawable(picture);
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}
EDIT:
two possible solutions
1
one idea I just thought of is somehow creating the Image on external memory like the SD card and using BitmapFactory to drop the size if it is large, before putting on the internal memory, how to actually do that is another question. the bitmap would have to be originally created on the external memory.
2
or is the memory problem only caused by loading it into an imageView. In that case I can drop the image with BitmapFactory Options before loading it in the imageView.
Bitmap bmp = Bitmap.createScaledBitmap(HTMLBitmap, 500, 300, false);
Looking at this example, Im trying to determine of the image in view is of CMYK colormode or not. If it it, I it must not try to display it, and continue with the next image in my list
This is how I am setting up my ImageView
options.inSampleSize = calculateInSampleSize(options, imageWidth, imageWidth);
imageView = (ImageView)findViewById(R.id.imageView);
Bitmap image = decodeSampledBitmapFromResource(path, 1280, 700);
imageView.setImageBitmap(image);
imageView.setVisibility(View.VISIBLE);
How do I determine if this image is CMYK ? Or simply said, I do I determine if this image can be displayed or not