Refresh ImageView from URL Android - java

I need some help. I want to refresh my ImageView loaded from a URL in my android app. I try use timers but doesn't work. Any thoughts?
I have this code in Main Activity:
public class Imagens extends Activity {
private ImageView imagem;
private Bitmap bit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagens);
imagem=(ImageView) findViewById(R.id.imageView);
bit=getBitmapFromURL("http://www.onlinedegrees.org/wp-content/uploads/android1.jpg");
imagem.setImageBitmap(bit);
}
public 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 (Exception e){
e.printStackTrace();
return null;
}
}
}
And in the AndroidManifest uses:
uses-permission android:name="android.permission.INTERNET"
to allow internet connection.

I can recommend a different way that works like a charm: Android Query.
You can download that JAR file from here
AQuery androidAQuery = new AQuery(this);
As an example:
androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);
It's very fast and accurate, and using this you can find many more features like animation when loading, getting a bitmap (if needed), etc.

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);

Thumbnail for video from server not getting displayed in ImageView

Need to generate thumbnail from video stored in server, below is my code but "path" variable is giving problem, how to get it resolved. If i remove path parameter with url parameter then i am getting the thumbnails but not in proper order or sometimes same thumbnail is generated for 2 or 3 videos, below is my code -
Video video = mVideos.get(position);
//play video using android api, when video view is clicked.
url = video.getVideoUrl(); // your URL here
Uri videoUri = Uri.parse(url);
new DownloadImage(holder.videothumbView).execute(url);
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = (ImageView ) bmImage;
}
protected Bitmap doInBackground(String... urls) {
Bitmap myBitmap = null;
MediaMetadataRetriever mMRetriever = null;
try {
mMRetriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
mMRetriever.setDataSource(path, new HashMap<String, String>());
else
mMRetriever.setDataSource(path);
myBitmap = mMRetriever.getFrameAtTime();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mMRetriever != null) {
mMRetriever.release();
}
}
return myBitmap;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
All the network related task must be done in a separate thread. You can't do it in main thread. You can use Picasso image library. It is open source and you can show different image for different state like loading, error etc.
Picasso.with(context) // your activity of other context referance
.load(video.getVideoUrl())
.placeholder(R.drawable.user_placeholder) // this will show during image loading from network
.error(R.drawable.user_placeholder_error) // error image to display
.into(holder.videothumbView);

How to set Image from Uri with a file:// scheme

I managed to use myimageview.setImageUri when the scheme was content://
But it doesnt work if my Uri has a file:// scheme. What is wrong and how can I make it work?
I have given both read and write permission.
Edit:
To make sure I haven't messed up anything, I created a whole new project.
Mainactivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
ArrayList<Uri> myimageUri = null;
myimageUri = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
Uri theuri = myimageUri.get(0);
ImageView myimageView = findViewById(R.id.imageView);
myimageView.setImageURI(theuri);
}
Attaching a debugger I found out that theuri is
file:///storage/emulated/0/WhatsApp/Media/WhatsApp%20Images/IMG-20171101-WA0005.jpg
It still doesn't work, but content :/// URIs work.
Try following code:
InputStream in = myimageView.g etContext().getContentResolver(). openInputStream(theuri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();
myimageView.setImageBitmap(bitmap);
You need to decode image file to a bitmap and give it to ImageView.
Bitmap mBitmap = Media.getBitmap(this.getContentResolver(), yourUri);
imageView.setImageBitmap(bitmap);

How to display an image with use of jar file in Android?

I am new for Android. I want to create an jar file if i call a method of that jar file in another android application it will show an image in imageview of the application. For example
a method in jar file as DisplayImg(URL); i will use this jar as library in another application and here i will create an imageview
ImageView imageView;
imageView = (ImageView) findViewById(R.id.img);
Then if i call imageView.DisplayImg(URL); then it will display specified url image on the imageview. I spend lot more time to get the solution but not work.plz help with the code thank
You don't need to create a library, in fact, there is a same library available for doing exactly what you want to!
It's called Picasso. You can find it here.
Also note that if you want imageView.DisplayImage(URL); you should create a new instance of ImageView class, because it doesn't contain this method. I recommend using DisplayImage(URL, imageView);.
But in case you REALLY WANT to do this, here you go:
1- For creating a library, create a New Android Application Project, and in the second window, check Mark this project as a library option. Also notice that we don't need Activity.
2- Create a new class, such as ImageUtils.java in the newly-created project. Here you need to put the DisplayImage() method:
public static void DisplayImage(String URL, ImageView imageView) {
new DownloadImageTask(imageView).execute(URL);
}
And this for the DownloadImageTask class :
public static class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
public DownloadImageTask(ImageView bmImage) {
this.imageView = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap imageBitmap = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
imageBitmap = BitmapFactory.decodeStream(in);
} catch (IOException e) {
// Probably there's no Internet connection. Warn the user about checking his/her connection.
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return imageBitmap;
}
protected void onPostExecute(Bitmap result) {
if (imageView != null) imageView.setImageBitmap(result);
}
}
3- Execute your project as an android application.
4- There should be a jar file under the bin folder. It's your library. Import it to your other applications and use it. In that applications you can use it with this code:
ImageView imageView = (ImageView) findViewById(R.id.img);
ImageUtils.DisplayImage(URL, imageView);
5- Don't forget to add this permission to the apps that use this library:
<uses-permission android:name="android.permission.INTERNET" />
Hope it helps :)

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.

Categories

Resources