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));
Related
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.
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);
I have an ImageButton that is initially set to a drawable resource. During an Activity, I want to set the image to a bitmap of a user photo. The picture taking and saving work correctly, but the ImageButton's image does not change.
Here is my code:
Bitmap bitmap = BitmapFactory.decodeFile(PATH + "/image.jpg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, stream);
Log.d(TAG, bitmap.toString()); //prints out android.graphics.Bitmap#41d772a8
rearEndImageButton.setImageBitmap(bitmap);
To do it form sd card you can try with..
Bitmap bmp = BitmapFactory.decodeFile("path_to_file");
ImageButton rearEndImageButton = (ImageButton)findViewById(R.id.rearEndImageButton);
rearEndImageButton.setImageBitmap(bmp);
In the activity onCreate method assign your button to a variable, and then set a resource or Bitmap which will be decoded from a file using the BitmapFactory class.
You can use File explorer to select an image from sd card and assign that image path to BitmapFactory.decodeFile().
I want to save an image in an ImageView to the database using R.id not R.drawable because the image can change(because its a camera picture). l can not use the path to the image to save because the gallery might be tampered around with.
HotOrNot entry = new HotOrNot(this);
byte[] image4 = HotOrNot.getBytes(BitmapFactory.decodeResource(null, R.id.imageView2));
entry.open();
entry.createEntry(image4);
entry.close();
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
You need to understand that, R.drawable.image is the ID of the actual image. And R.id.imageView2 is the ID of the ImageView.
Your argument is that the image you want to store can change. So in that case, what you can do is use yourImageView.getDrawable(), so that you will get the current image in the ImageView and then convert it to Bitmap using this
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
byte[] image4 = HotOrNot.getBytes(bitmap); // Your code from here.
Whatever is represented by R.id.. is an id for a resource type from the project's pre compiled assets. I don't think you can get an id value for an image taken with the Camera.
Also note that the type of R.id.. is an int.
I know the above is not an answer. If you want to take an image using the Camera and save it in the database, check this question in SO. Hope it helps.