Im trying to load an image from url to my android ImageView. but it gives no image for my url. but when i call another sample url it loads on the ImageView
My URL which gives empty
https://192.168.100.15/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102
This sample URL works for me
https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png
This is the code i am working on
public class GetImage extends Activity{
ImageView postpic1;
Bitmap b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_posts);
postpic1 = (ImageView) findViewById(R.id.postpic1);
information info = new information();
info.execute("");
}
public class information extends AsyncTask<String, String, String>
{
#Override
protected String doInBackground(String... arg0) {
try
{
URL url = new URL("https://localhost/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102");
InputStream is = new BufferedInputStream(url.openStream());
b = BitmapFactory.decodeStream(is);
} catch(Exception e){}
return null;
}
#Override
protected void onPostExecute(String result) {
postpic1.setImageBitmap(b);
}
}
}
The URL for your image is localhost. Localhost(127.0.0.1) refers to same machine as the request origination. So, your phone sends request to itself. Instead specify the IP address of your pc where the server is running.
PS: Ensure both your PC and your phone are connected to the same network.
I think that your problem is in your URL, replace your localhost with your IP address, hope it solves your problem.
Just use image loading and caching libraries. For instance Picasso
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Alternative solution is Glide; It has a similar working principleIt has a similar working principle:
Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
Do you have a local webserver running which supports HTTPS? Because that is where you are trying to load an image from.
Also, if you have on running, do you get the image when you call your desired URL in your browser?
Have you tried using Picasso library very easy and effective:
Go to you build.gradle inside app dir and add to dependencies:
compile 'com.squareup.picasso:picasso:2.5.2'
Then use picasso lib:
String url = "https://localhost/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102";
Picasso.with(context) //The context of your activity
.load(url)
.into(postpic1);
Related
I am using the Picasso library to set the image from URL. This URL is working in other programming language but not in Android.
Picasso.with(context).load(product_modal.getImage()).placeholder(R.drawable.ic_no_image).into(holder.iv_thumbnail_filled);
Finally, I found the actual problem that you have faced. Replace https to http in your URL. Because your site does not have SSL.
Just created a method for loading image
private void loadImage(final ImageView imageView, final String imageUrl){
Picasso.get()
.load(imageUrl)
.placeholder(R.drawable.image_white)
.into(imageView , new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
String updatedImageUrl;
if (imageUrl.contains("https")){
updatedImageUrl = imageUrl.replace("https", "http");
}else{
updatedImageUrl = imageUrl.replace("http", "https");
}
loadImage(imageView, updatedImageUrl);
}
});
}
You just need to provide an imageView and the image URL. For the first time if image not loaded then its try to replace https to http and then try load the image.
Using the method by using this:
loadImage(holder.iv_thumbnail_filled, product_modal.getImage());
And make sure you have Internet Access Permission on AndroidMenifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Hope this will solve your problem.
According to Picasso library, they've changed the way of loading images
Please use the below to load image
Picasso.get().load(product_modal.getImage()).placeholder(R.drawable.ic_no_image).into(holder.iv_thumbnail_filled);
You can check this question for more info.
If you have HTTP 504 error, have a try:
Uninstalling the app and installing it again!
Info from here.
I'm trying to download the following image using Picasso https://s3-media4.fl.yelpcdn.com/bphoto/94E7Ti0RTDbA6mGotZw5DA/o.jpg
I can see it fine in a browser. However when I attempt to download it using Picasso, I get an error (a breakpoint in my onError() method gets hit).
This is an extract of my code:
final RequestCreator rc = with(context).load(fullImagePath);
if (fit != null && fit) {
rc.fit();
}
// If no callback listener exists, create one.
if (callbackListener == null) {
callbackListener = new Callback() {
#Override
public void onSuccess() {
L.p("onSuccess retrieving " + fullImagePath);
}
#Override
public void onError() {
// Something went wrong
L.p("Error retrieving " + fullImagePath);
}
};
}
rc.into(fImageView, callbackListener);
I found this: https://github.com/square/picasso/issues/500 however it's a bit dated and the OkHttpClient class no longer has the setProtocols() method.
Thanks!
Inserting that link as "http" instead of "https" will work.
The below code works for me for both http and https links.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
String https = "https://s3-media4.fl.yelpcdn.com/bphoto/94E7Ti0RTDbA6mGotZw5DA/o.jpg";
String http = "http://www.sunseed.org.uk/projectpack/files/2014/05/Library_1400_800-1200x686.jpg";
Picasso.with(this)
.load(https)
.placeholder(R.drawable.me)
.into(imageView);
}
Also be sure you write the below permission in manifest file:
<uses-permission android:name="android.permission.INTERNET" />
also write the following in build.gradle file
implementation 'com.squareup.picasso:picasso:2.5.2'
I am using Picasso library to get Image from an url.
My problem is when I load an Image for the first time and I exit out of my app and after I come back my app tries to load the Image again but I don't want it happen. Are there any other way to do that(load just one time the Image and in others time don't need to Internet for load)?
The issue with the above answers is that they only check the availability of the images in the disk cache, it does not cover the part if the image does not exist in the cache to go online and retrieve it.
First make a class that extends Application (You can name it whatever you want that does not interfere with your application, my convention is to use "Global").
public class Global extends Application {
#Override
public void onCreate() {
super.onCreate();
Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(new OkHttpDownloader(this,Integer.MAX_VALUE));
Picasso built = builder.build();
built.setIndicatorsEnabled(false);
built.setLoggingEnabled(true);
Picasso.setSingletonInstance(built);
}
}
Make sure you add dependancy for OkHttp library, it's developed by the same guys from Picasso
compile 'com.squareup.okhttp:okhttp:2.4.0'
and add the class in your Manifest file Applications tag :
android:name=".Global"
Then when you want to retrieve the image :
Picasso.with(context)
.load(Image URL)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
//Try again online if cache failed
Picasso.with(context)
.load(Image URL)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Log.v("Picasso","Could not fetch image");
}
});
}
});
The above method checks if the image is already cached, if not gets it from the internet.
try this:
Picasso.with(this).load(url).networkPolicy(NetworkPolicy.OFFLINE).into(imageView);
try this:
Picasso.with(this).invalidate(url);
Picasso.with(this)
.load(url)
.networkPolicy(
NetworkUtils.isConnected(this) ?
NetworkPolicy.NO_CACHE : NetworkPolicy.OFFLINE)
.resize(200, 200)
.centerCrop()
.placeholder(R.mipmap.ic_avatar)
.error(R.mipmap.ic_avatar)
.into(imageView);
I want to download the following image downloading code with Picasso image cache.
DownloadImage downloadImage = new DownloadImage();
downloadImage.execute(advert.getImgUrl());
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... arg) {
Bitmap bmp = null;
try {
URL url = new URL(arg[0]);
bmp = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return bmp;
}
#Override
protected void onPostExecute(Bitmap result) {
if (result == null) {
Intent intent = new Intent(AdvertisingActivity.this,
AdvertisingErrorActivity.class);
intent.putExtra("ad-error", "Error downloading image");
}
adImg.setImageBitmap(result);
super.onPostExecute(result);
}
}
I have several questions regarding this.
I want to download more than one image in parallel. If I make repeated calls of Picasso.with(getActivity()).load(url); with different url values, does this get done?
I want to download images in one activity and use it in another activity. Is this possible? How can this be done?
If I call Picasso.with(getActivity()).load(url); more than once with the same url value, does this load the cached images for subsequent calls after the image has been downloaded?
If the image download process does not succeed for some reasons, can you make Picasso report you of the failure?
I've researched some more into your questions and decided that I should publish this as an answer rather than a comment.
Yes - Picasso loads images asynchronously so making repeated calls will cause images to be downloaded in parallel.
Yes - just make the call as normal and Picasso will handle the re-use of downloaded images e.g. in Activity1, call Picasso.with(this).load("image1"); and, later, make a call to the same URL in Activity2. The image will already be cached (either in memory or on device storage) and Picasso will re-use it, rather than downloading it again.
Yes - see above (Picasso will automatically use cached images where available)
This does not seem to have such a clear-cut answer. One thing you can do is provide an image to display if an error occurs while fetching the real image:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
The 'placeholder' will be displayed whilst the attempt is being made to fetch the image from the web; the 'error' image will be displayed, for instance, if the URL is not valid or if there is no Internet connection.
Update, 17/03/2014:
Picasso supports the use of a callback to report you of a failure. Modify your usual call (e.g. the above example) like so:
.into(imageView, new Callback() {
#Override
public void onSuccess() {
// TODO Auto-generated method stub
}
#Override
public void onError() {
// TODO Auto-generated method stub
}
});
In conclusion, it sounds like Picasso would be a great choice of library for you. It definitely makes image downloading very quick and very easy, so I like it a lot.
I am writing an Android Application in Eclipse in which i need to write some text to a Text file on my webserver. In the onCreate() I am writing something like this..
try{
String data = "hello";
URL u1 = new URL("http://url.com/script.txt");
URLConnection uc1 = u1.openConnection();
uc1.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(uc1.getOutputStream());
out.write(data);
out.flush();
}
catch( Exception e ) {
e.printStackTrace();
}
But its not working. I just need to write a simple text onto an existing Text file on my Web Server. I know its a simple task but I have tried several ways and spent sometime on google trying to do this but not working..Some times I receive errors in yellow:
05-16 16:22:24.853: W/System.err(29095): android.os.NetworkOnMainThreadException
05-16 16:22:25.563: W/System.err(29095): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
...
...
...
I tried writing in Async also:
private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
ProgressDialog mProgressDialog;
#Override
protected void onPostExecute(Void result) {
}
#Override
protected void onPreExecute() {
}
#Override
protected Void doInBackground(Void... params) {
//code comes here.....
}
}
But nothing is working.. i have android.permission.ACCESS_NETWORK_STATE and android.permission.INTERNET enabled in manifest. My folders in server hav write permissions.
Any help is appreciated... Thanks
You cannot access file served by some server like they resources on your file system.
Use HTTP POST with data and have the file writing done by some script engine like PHP.
The use of AsyncTask is required as well.
This is an issue with implementation on your webserver, not Android. One does not simply write over HTTP. You need something like a REST API or a specialized protocol.
Write permissions on the server only mean that the current user on the server has permissions to write to those folders. Outside clients still can only read unless you have something like a PHP script that accepts a POST variable with the text you want to write.