I am using Jsoup to scrape a gallery of pictures from this italian website
http://www.italiaebraica.org/index.php?option=com_phocagallery&view=category&id=3:famiglia-levi&Itemid=143&lang=it
in an AsyncTask with Jsoup i'm getting from the HTML all the urls of the images:
#Override
protected Void doInBackground(String... params) {
Document doc;
try {
ConnectivityManager conMgr = (ConnectivityManager) mActivity
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
doc = Jsoup
.connect(urlReceivedToConnect)
.timeout(0).get();
Elements imgList = doc.getElementsByClass("phocagallery-box-file-third").select("img");
photoList = new ArrayList<String>();
ListIterator<Element> post = imgList.listIterator();
while (post.hasNext()) {
photoList.add(post.next().attr("abs:src"));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Then, in a costumized adapter, i'm taking this urlsList and i'm loading the images from the url that i'm putting in a gridView later:
private Drawable LoadImageFromURL(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (Exception e) {
System.out.println(e);
return null;
}
}
The problem is: some of the pictures are shown and are ok, but some others presents this error:
06-23 10:06:06.930: I/System.out(493): java.io.FileNotFoundException: http://www.italiaebraica.org/images/phocagallery/famiglia_levi/thumbs/phoca_thumb_m_Famiglia Levi 024.jpg
what's the problem? how can I get all the pictures in the right way?
Please help,
hope it is clear ,
i'm a junior developer!!
java.io.FileNotFoundException:
is pretty self-explanatory. Print out the urls so you can see the ones that cause the exception. It shouldn't take too long to debug.
I'm don't know what images exist and what don't, so you're the one who has to figure it out.
What is wrong is there are spaces in the URL. Most browsers are made to detect if there is a space and replace it with %20 so you won't get any error going to the URL in a browser. So I would recommend using:
private Drawable LoadImageFromURL(String url) {
if(url.contains(" ")){
url.replace(" ", "%20");
}
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (Exception e) {
System.out.println(e);
return null;
}
}
Related
Provided I have an array of image URLs, I am trying to download all these images one-by-one using glide. Presently, I am able to download a single image when I provide its URL. And here is the code:
private void downx()
{
File sd = getExternalCacheDir();
File folder = new File(sd, "/mobio/");
if (!folder.exists()) {
if (!folder.mkdir()) {
Log.e("ERROR", "Cannot create a directory!");
} else {
folder.mkdirs();
}
}
final File[] fileName = {new File(folder, "one.jpg"), new File(folder, "two.jpg"),new File(folder, "three.jpg")};
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params)
{
try
{
theBitmap = Glide.
with(getApplicationContext()).
load(urls[2]).
asBitmap().
into(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL).
get();
}
catch (final ExecutionException e)
{
Log.e("TAG", e.getMessage());
}
catch (final InterruptedException e)
{
Log.e("TAG", e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(Void dummy) {
if (null != theBitmap) {
// The full bitmap should be available here
Log.d("TAG", "Image loaded");
Log.e("GLIDE","I am Ready");
try {
FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName[1]));
theBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.execute();
}
Now the problem is: What approach do I adopt if I need to download multiple images, and how do I force my code to adapt to handle multiple downloads?
I have used a class which takes a list of URLs and downloads the Images to a file. Please check this Gist for more. This uses Picasso to download Images but you can edit the download code to use glide as well. Should be a one line change. Hope this helps.
https://gist.github.com/bpr10/a765a015bf1c774816ba58c7ae6413d6
I guess it will be better to use Download Manager as it will handle queuing, network availability etc.
I have a Parse Android app for which I am implementing Facebook sign up. Currently I am stuck on grabbing images to set as profile pictures of new ParseUser's. I have successfully used the Facebook Graph API to retrieve the correct URL (I have checked this by plugging it into a browser, where I am shown the right profile picture), but I now need a way to turn that URL into a byte array (byte[]) so that I can save the ParseFile field of our ParseUser's profile picture. I have already looked at all these SO questions:
• java.net.URL read stream to byte[]
• Efficiently read file from URL into byte[] in Java
• Get image with given url and convert it to byte array
None of these have worked. I am currently trying to use the Apache IOutils, like in the solution from the second link. Here is my current code for the AsyncTask:
private class SetProfPicWithURL extends AsyncTask<URL, Integer, byte[]> {
#Override
protected byte[] doInBackground(URL... imageURL) {
Log.i("SetProfPicWithURL", "invocation, URL: " + imageURL[0]);
InputStream is = null;
byte[] bytes = null;
try {
is = imageURL[0].openStream();
bytes = IOUtils.toByteArray(is);
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (is != null) try {
is.close();
if(bytes == null){Log.e("LoginActivity", "bytes is null int SetProfPicWithURL");}
final ParseFile imageFile = new ParseFile("image.jpg", bytes);
imageFile.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("LoginActivity", "getCurrentUser.put");
ParseUser.getCurrentUser().put(ParseUtils.PARSE_PROFILE_IMAGE, imageFile);
ParseUser.getCurrentUser().saveInBackground();
} else {
e.printStackTrace();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}
}
Now when this code executes, I get no error logs, and a ParseFile is created. However, no profile pictures load within the app, and when I click to examine the file in the dashboard, I get this error message:
The file “tfss-0280f98d-7180-4528-9d24-3ec47d3b25d4-image.jpg” could
not be opened because it is empty.
Honestly, I'm at a loss. I've spent significantly more time on this one photo issue than any other part of implementing the Facebook login. And the way our database is set up, it is really not ideal to create another field to save the URL and load with Picasso. Any help with this issue is truly appreciated!
Directly save your imagefile as profile picture like this :
final ParseFile imageFile = new ParseFile("image.jpg", bytes);
ParseUser.getCurrentUser().put(ParseUtils.PARSE_PROFILE_IMAGE, imageFile);
ParseUser.getCurrentUser().saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("LoginActivity", "Profile saved succesfully");
} else {
e.printStackTrace();
}
}
});
EDIT :
Use this to get image byte array from url.
try {
java.net.URL img_value = new java.net.URL(imageURL);
Bitmap mIcon = BitmapFactory
.decodeStream(img_value.openConnection()
.getInputStream());
if (mIcon != null)
imgByteArray = encodeToByteArray(mIcon);
} catch (Exception e) {
e.printStackTrace();
}
public byte[] encodeToByteArray(Bitmap image) {
Log.d(TAG, "encodeToByteArray");
Bitmap b= image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imgByteArray = baos.toByteArray();
return imgByteArray ;
}
Good day,
I'm trying to retrieve an image using Jsoup but I'm unsure as to what exactly I should be getting from the website. I've used the following code to read from the website and have been able to get the images particular title and the URL it links to but not the image.
I want to set this image to the ImageView that I have in the activity. Here's my code thus far:
// Get the required stuff from the webpage
Document document = null;
try {
document = Jsoup.connect(URL).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element info = document.select("div.featurebox").first();
// Caption on image
docInfo = info.text();
// URL of image
imageURL = info.attr("data-url");
// Retrieve the actual image
Element featureImage = document.select("div.featurebox-image").first();
// Unsure what to get here
It should be noted that the image isn't stored as a normal img-src way. The particular div class I'm looking at is this:
<div class="featurebox-image" style="background:url(http://img.mangastream.com/cdn/feature/02.jpg) center center;">
<div class="featurebox-caption">
<strong>History's Strongest Disciple Kenichi <em>544</em></strong> - Witch </div>
</div>
So I'm after the actual image from that URL.
How do i go about this?
Thanks
Thanks to Hardip Patel for providing the start. Here is what I did:
I took Hardips code and changed it to the following:
Element featureImage = document.select("div.featurebox-image")
.first();
String temp = featureImage.getElementsByAttribute("style")
.toString();
// URL of image
imageStrg = temp
.substring(temp.indexOf("(") + 1, temp.indexOf(")"));
After that it took alittle looking about StackOverflow to find out how to set it. I initially tryed to set it using the URL using the setImageURI() method, but that was throwing an error. See here for why. Instead I used that SoH's answer to create a bitmap from the URL:
// Method to return a bitmap from an images URL
private Bitmap getImageBitmap(String url) {
Bitmap bm = null;
try {
// See what we are getting
Log.i(TAG, "" + url);
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
}
After that I just had to set the Bitmap from earlier and update the image view using the ASyncTask's onPostExecute() method:
imageOne = getImageBitmap(imageStrg);
#Override
protected void onPostExecute(String result) {
// Write the result (document title) to the textview
super.onPostExecute(result);
// Update the textview with results
if (result == null) {
txtVwDocTitleValue.setText("Nothing to report...");
} else {
txtVwDocTitleValue.setText(result);
txtVwDocURLValue.setText(imageURL);
// Set the views image
imgVwManga1.setImageBitmap(imageOne);
}
// Destroy the progress bar
stopProgressDialog();
}
Cheers all!
See if this works :-
String temp = featureImage.getAttribute("style");
String url = temp.substring(temp.indexOf("(")+1,temp.indexOf(")"));
Try this
Document doc = Jsoup.connect("www.mywebsite.com").get();
Elements images = doc.select("img[src~=(?i)\.(png|jpe?g|gif)]");
I am currently designing an application that needs to plot a route on a MapView. For this to function correctly I need to get data from a KML document that i get from:
http://maps.google.com/maps?f=d&hl=en&saddr=-33.882993,18.63486&daddr=-33.870162,18.657837&ie=UTF8&0&om=0&output=kml
I have created a test project to ensure that this data is received correctly. The problem is that it runs perfectly on my emulator, but not on the actual android phone.
The following piece of code starts a thread when a button is clicked, getting an input stream response (KML).
public void onClick(View v) {
if (v.getId()== R.id.btn1)
{
new Thread() {
#Override
public void run() {
String url = "http://maps.google.com/maps?f=d&hl=en&saddr=-33.882993,18.63486&daddr=-33.870162,18.657837&ie=UTF8&0&om=0&output=kml";
InputStream is = getConnection(url);
mRoad = RouteProvider.getRoute(is);
mHandler.sendEmptyMessage(0);
}
}.start();
}
}
private InputStream getConnection(String url) {
InputStream is = null;
try {
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
conn.connect();
is = conn.getInputStream();
conn = null;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
For the moment, all getRoute() needs to do is give the contents of the "LineString" element inside the KML Document. These contents are a list of coordinates that can be used to draw a route.
public class RouteProvider {
/** Gets data from KML response **/
public static Road getRoute(InputStream is) {
Road road = new Road();
try
{
Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
NodeList a = xmlDoc.getElementsByTagName("LineString");
System.out.println(a.item(0).getTextContent());
road.mName = a.item(0).getTextContent();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return road;
}
On the emulator I am running the correct value is displayed within road.mName, which is a list of coordinates. On a Android phone it displays null?
I am building for Android 2.3.3 on eclipse and I used a Samsung Galaxy S2 for testing.
Did not need to use KML after all. Just used google maps to supply xml format. Much easier.
https://developers.google.com/maps/documentation/directions/
I am trying to download image from server. Few are downloading and few and creating problem. I don't know why.
I have downloaded and show image to user on the same location. Here is the file which is able to download.
http://www.mongreldog.co.nz/unilogo/Backgrounds_20399.png
When I am trying to download following image. This image is opening in browser but not downloading in android
http://www.mongreldog.co.nz/unilogo/Twitter-Ryan_Giggs_Imogen_Thomas_Guard-Footballer_Affair_UK_Manchester%20United_M_785.jpg
Its give exception
java.io.FileNotFoundException: http://www.mongreldog.co.nz/unilogo/Twitter-Ryan_Giggs_Imogen_Thomas_Guard-Footballer_Affair_UK_Manchester United_M_785.jpg
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
at src.com.mongreldog.appsupport.Utils.downloadImage(Utils.java:77)
at src.com.mongreldog.ViewFullCompAndCommentActivity$3.performInBackground(ViewFullCompAndCommentActivity.java:607)
at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:44)
at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
Here is my code.
public static Bitmap downloadImage(String imageURLStr) {
Bitmap bitmap = null;
InputStream in = null;
try {
URL url = new URL(imageURLStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
in = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (SocketTimeoutException e) {
bitmap = null;
e.printStackTrace();
} catch (IOException e) {
bitmap = null;
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
bitmap = null;
} catch (OutOfMemoryError e) {
e.printStackTrace();
bitmap = null;
}
return bitmap;
}
Use URL Encoder to encode the image URL, as you can see the URL have white spaces in the log report.
public static Bitmap downloadImage(String imageURLStr) {
imageURLStr = URLEncoder.encode(imageURLStr, "utf-8");
//... rest of your code.
}
Edit: as you reported of issue '+' instead of %20
you can use
public static Bitmap downloadImage(String imageURLStr) {
imageURLStr = imageURLStr.replaceAll(" ", "%20");
//... rest of your code.
}
For source check here
I try to use URLEncoder.encode() to encode the URL. Its strange that it convert " " with "+".
Please try Uri.encode(imageURL). I just try it and its working perfectly.
I have tested that in android.
Looks like the file name is not being parsed correctly (the space between 'Manchester' and 'United').
Use URLEncoder.encode() to encode the URL.
Your second URL is very instable. It may return 404 in the most cases even in Chome. I have seen the picture only once.