How to convert url to image to Image? - java

I get url to my Image, and want it make Image;
I want use this methods
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);
or
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
Image image = ImageIO.read(url);
but Android Studio can't find ImageIO and java.awt.Toolkit. How can I add them?

You can get Bitmap from url and then you can use that bitmap further to set image of an ImageView.
URL url = new URL("http://....");
Bitmap image=BitmapFactory.decodeStream(url.openConnection().getInputStream());

Related

Using an AsyncTask to update a user profile image in Android

I have a problem with the user profile picture update in my android application.
Introduction
In my MainActivity I can switch between two Fragments, the first one is the HomeFragment and the second is the UserProfileFragment.
In the UserProfile, the user can decide to upload a new image, so i let the user pick the image from local storage, then i upload this picture into FirebaseStorage and get the download link.
I use a class called LoadImageTask extending an AsyncTask that download the image from url and then upload the resulting bitmap into an ImageView that i have in my UserProfileFragment.
The problem:
When a user uploads a new profile picture, LoadImageTask does his job correctly. The problem is that, when the user tries to update his profile picture, the ImageView is still showing the old image and the user must go to HomeFragment and then go back to the UserProfileFragment to see the update. So, basically the update works but the Profile Picture change is not showed immediately.
What i tried to do
I tried many things like invalidating the imageView or trying to force the imageView in other ways but it didn't worked.
public class LoadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView image;
public LoadImageTask(ImageView image) {
this. image = image;
}
#Override
protected Bitmap doInBackground(String... urls) {
String downloadUrl = urls[0];
Bitmap bitmap = null;
try {
java.net.URL url = new java.net.URL(downloadUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
if(result != null) {
image.setImageBitmap(circleTransformation(result));
}
}
}
When you pick image from the local storage you can set image to imageView from the uri in your onActivityResult.
imageView.setImageURI(imgUri);

How can I add the image using sd card image path and apply it on the profile picture image?

I have created the following navigation drawer layout but now I want to apply the sd card image on profile picture view. How can I apply it?
ScreenShot:
1) Read your image from the sd card:
File image = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "path_to_your_file");
if(image.exists())
Bitmap bmp = BitmapFactory.decodeFile(image.getAbsolutePath());
2) Set it into your appropriate ImageView
ImageView imgProfile = (ImageView) findViewById(R.id.your_image_view_id);
imgProfile.setImageBitmap(bmp);
Getting Image from sdcard
Bitmap bmp = BitmapFactory.decodeFile(pathName);
ImageView img;
img.setImageBitmap(bmp);
//add permission AndroidManifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

viewpager imageview zoomin/zoomout landscape capture photo display not full width of imageview

I am using viewpager to view every single photo inside imageview using this :-
ZoomableImageView imageView = new ZoomableImageView(context);
Bitmap bimage= getBitmapFromURL(test);
imageView.setBitmap(bimage);
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
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;
}
}
the problem I am facing right now is that when i take photo in landscape mode and view the photo in landscape, the photo in imageview is not shown in full width of the devices. But when i capture it in portrait mode, it comes in full size and in landscape, it is not full size but centralized correctly. i wants to show full width for landscape photo just like the android native gallery.
Any idea?
Thanks in advance.

Android: ImageView and SD-card

I am trying to read an image from /mnt/sdcard/img.jpg into ImageView.
Bitmap bm = BitmapFactory.decodeFile("/mnt/sdcard/img.jpg");
imageView1.setImageBitmap(bm);
I have write external storage permission.
But ImageView is empty, LogCat don't get any errors,
How can I fix it?
Try
File file = new File("/mnt/sdcard/img.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview1.setImageBitmap(bitmap);

Loading drawable from sd card

I'm trying to load a png image as a drawable from my device sd card.
I use the following function but it doesn't work:
public Drawable getDrawable()
{
return new BitmapDrawable(imagePath);
}
The image path is: mnt/sdcard/MyFolder/image.png
The app crashes when I try calling that method, how should I load my png image located in my sdcard and cast it into a Drawable object?
There is actually a BitmapDrawable constructor straight from file path. The method you are using is depricated. Try:
Drawable myDrawable = new BitmapDrawable(getResources(), pathName);
If this doesnt work, Try getting a bitmap and creating a drawable from it:
The bitmap can be created with decodeFile
You can use it like this:
Bitmap myBitmap = BitmapFactory.decodeFile(pathName);
Then you can use the bitmap for drawing etc.
to convert Bitmap to drawable use
Drawable myDrawable = new BitmapDrawable(getResources(), myBitmap);
Take a look Here (Bitmaps) and Here (Bitmap Drawables) for more info.
I am simple do like that
public Drawable getDrawableFromPath(String filePath) {
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
//Here you can make logic for decode bitmap for ignore oom error.
return new BitmapDrawable(bitmap);
}

Categories

Resources