Reputation: 9507
Hello i use Loading Images for Loading Images. But my images are not load. I work on that and develop one Demo program that is as follows.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new DownloadImageTask((ImageView) findViewById(R.id.my_image))
.execute("http://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Raw_Conditioner_500835f701532_175x175.jpg");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(ImageLoadExampleActivity.this);
pd.show();
}
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
pd.dismiss();
}
}
It does not work. But if i change URL of image at that time image is loaded successfully. My question is this perticular url is not loaded in my android application. Please help me to find this. I spend one days on this. Thanks in advance.
-Hardik
Upvotes: 1
Views: 509
Reputation: 1287
public String loadImageFromWebOperations(String url, String path) {
try {
is = (InputStream) new URL(url).getContent();
System.out.println(path);
File f = new File(path);
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
try {
byte[] b = new byte[100];
int l = 0;
while ((l = is.read(b)) != -1)
fos.write(b, 0, l);
} catch (Exception e) {
Log.e("Error in creating file", "", e);
}
Log.e("created file path is :", "" + f.getAbsolutePath());
return f.getAbsolutePath();
} catch (Exception e) {
System.out.println("Exc=" + e);
return null;
}
}
If any issue let me know...
Upvotes: 1
Reputation: 9507
After spending 2 days i finally come here to give answer on my own question. The main problem is of file names. I change file names and file location of files and my issue is solve. Thanks all which give me idea.
One also thing which i come to know that i modify my http:// with https://
Upvotes: 1
Reputation: 8358
I use the following to get images from an endpoint
URL url = new URL(imageUrl);
InputStream content = (InputStream)url.getContent();
Drawable image = Drawable.createFromStream(content , "src");
perhaps you can adapt this
Okay I downloaded the sample and looked at URLs - the issue appears to be the HTTP => HTTPS redirection (I mentioned that I had to change them in my sample - I also notice my browser was forced to switch to https when I tried to load your images from the supplied strings). The sample does not appear to handle that correctly however if I alter ImageLoader.getBitmap to this then it does
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
try {
Bitmap bitmap=null;
HttpURLConnection conn;
URL imageUrl = new URL(url);
int rc = -1;
do {
conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
rc = conn.getResponseCode();
imageUrl = conn.getURL();
} while (rc == -1); // hmmm - perhaps a http => https
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
return null;
}
}
Upvotes: 2