Bad issue with loading image from url - java

Solved
my problem was that my imagelink string was null sometimes, and i was bcz of bad getting data!
I have an image link which i want to download it into bitmap, this but i cant because the bitmapdownloader UniversalImageLoader both shows null, but the link is perfectly correct and working.
Im getting them from a wordpress website which the link is:
http://wikirap.ir/?get_all
i face this problem sometimes for some post photos in website. so i faced it again for a post photo(i mean the imgloaders was showing null) and i decided to put another photo(which it was loading correctly in app) for the post! but guess what? it didnt load too! but when i was opening the other post with same photo(in app) its loading!
this problem is making me crazy badly!
Update:
i changed my bitmap download code but my first code was saying this error
"... Bitmap.getconfig() on null object reference"
Help me to get rid of this please.
The thing i found out is that the photo shows as preview photo in list but when i click to see details it shows blank!(really bullshit)
Update Code :
Json getting : well i get it with okhttp and its ok and no need guys.
Showing in list: I use json and lazy list adapter which is working perfect and showing all photos even those i cant load them in detail page.
(preview image link is the same as detail image link)
detail page code getting bitmap:
protected void onPostExecute(String file_url) {
try {
actionBar.setTitle(name1.toString() + " - " + name2.toString());
tvinfo.setText(likes + " Likes | " + views + " Views");
}catch (NullPointerException e)
{
}
try
{
/**
URL url = new URL(Imagelink);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmaporigin = BitmapFactory.decodeStream(input);
**/
imgLoadercover imglcover = new imgLoadercover(ac.getApplicationContext());
imglcover.DisplayImage(Imagelink, R.color.placeholder, cover);
}catch (Exception e)
{
}
}

Related

I'm getting Http 400 Error from URLs string containing "%20" with Java Net URL

Hey all trying to write a web scraper that downloads all the songs from this website
https://billwurtz.com/songs.html
but some of his older URLs contain spaces ("%20") such as https://billwurtz.com/can%20i.mp3. Which on his newer links he has changed to '-'.
Anyway, when I try to download these tracks I get a 400 Error (Bad Request Error) which makes me think that java is sending the request with and not %20
URL website = new URL("https://www.billwurtz.com/" + song);
Path out = Path.of("songs/" + song);
System.out.println(website.toString());
try (InputStream in = website.openStream()) {
Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
// missed.add(song + ":" + e.getMessage());
throw new Exception(e.getMessage());
}
returns
https://www.billwurtz.com/can%20i.mp3
Exception in thread "main" java.lang.Exception: Server returned HTTP response code: 400 for URL: https://billwurtz.com/can i.mp3
I tried looking about but only came across issues created from the programmer trying to create a URL that contains (spaces) but here it is clear that it is using %20
Thank you all for your time :)

Brother SDK Calling PrintImage causes JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring

Currently working with the Brother SDK for a label Printer and when I call Asset Manager, the Bitmap appears but when it is parsed to print image it crashes.
if (myPrinter.startCommunication()) {
AssetManager assetManager = getAssets();
Bitmap bitmap = null;
try{
bitmap = BitmapFactory.decodeStream(new BufferedInputStream(assetManager.open("meme.bmp")));
}catch(Exception e){
e.printStackTrace();
}
PrinterStatus result = myPrinter.printImage(bitmap);
if (result.errorCode != ErrorCode.ERROR_NONE) {
Log.d("TAG", "ERROR - " + result.errorCode);
}
myPrinter.endCommunication();
}
Stack Readout
Debugger printout, with Image Preview.
For anyone got stuck even if they made sure they have the write permission granted, it was about the working path b-PAC trying to create.
The default path is: /storage/emulated/0/com.brother.ptouch.sdk/template
Just define your own path in PrinterInfo object. Set the workPath variable as such:
PrinterInfo mPrintSettings = mPrinter.getPrinterInfo();
mPrintSettings.workPath = "your/custom/work/path";
It says in the document that you don't even need the permission if you do so. But I'm going to keep it anyway.
I had this issue as well. Have you made sure that the android.permission.WRITE_EXTERNAL_STORAGE permisson is granted.
Having this in the manifest file wasn't enough, but by requesting permisson from the user, this specific error went away.
Credit to this post on SO

Android: I am unable to download an image with the URL I have to use due to formatting issues

I am using an API that returns a String for the URL of a photo. It is in a strange format, however, and it is causing me issues downloading the images using Volley (or any other method for that matter).
My code looks like this:
imageview = (ImageView) findViewById(R.id.imageView);
String web_url = "http:\\/\\/static.giantbomb.com\\/uploads\\/square_avatar\\/8\\/87790\\/1814630-box_ff7.png";
String web_url2 = "http://www.finalfantasyviipc.com/images/media_cloud_big.jpg";
ImageRequest ir = new ImageRequest(web_url2, new Response.Listener<Bitmap>() {
#Override
public void onResponse(Bitmap response) {
imageview.setImageBitmap(response);
Log.d("image was ", "set");
}
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "ERROR: " + error.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("ERROR", error.getMessage());
}
}, 0, 0, null, null);
requestQueue.add(ir);
As you can see, the second URL works fine, but the first URL, the one with the many backward and forward slashes, will not return an image. You can copy both into your browser and they work fine, but the first one cannot be read by my Android parsing. Does anyone have any recommendations of how to get an image from the first link? Nearly everything I have tried utilizes Strings, which seems to be a core part of the problem.
Thanks for your help!
-Sil
You can copy both into your browser and they work fine, but the first one cannot be read by my Android parsing
The first URL is definitely malformed but the reason why it works in browsers is because they automatically convert backslashes to forward slashes and most web servers tend to ignore multiple consecutive forward slashes.
For example, if I enter that URL in Chrome (OSX), the URL in the address bar changes to:
http://static.giantbomb.com///uploads///square_avatar///8///87790///1814630-box_ff7.png
which seems to work fine. So, to solve your problem on Android, just do the same:
web_url = web_url.replace("\\", "/");
or even better:
web_url = web_url.replace("\\", "");
That should convert the URL to http://static.giantbomb.com/uploads/square_avatar/8/87790/1814630-box_ff7.png and therefore fix your issue.

detecting missing jpegs over the internet

The problem my application reads in jpegs and displays them on a jlabel (these are pictures of books)
Everything works fine when used with the local version e.g. reading from the C drive, but once I try to do this over the internet problems occur that I have tried without success to correct
Scenario
Should the jpeg not be present at the end of the url I get the following error
javax.imageio.IIOException: Can't get input stream from URL!
In the version that reads from the local drive I detect if the file exist and overcome this problem however I have tried lots of the ideas posted and I simply can’t find out how to detect that the jpeg is absent!
Please can some one help
here are the two version of the code
Read from local drive C
private void showcover() {
String stockPic;
String partofISBN;
String completeurl;
jButton9.setVisible(true);
stockPic = jTextField1.getText();// get the current isbn
partofISBN = stockPic.substring(0, 7); // get first 7 numbers
String picUrl;
stockPic = stockPic + localNumber + ".jpg";
picUrl = partofISBN + "\\" + stockPic;
completeurl = "C:\\Apicture\\" + picUrl;
File pf = new File(completeurl);
if (!pf.exists()) {
jLabel9.setIcon(new ImageIcon("C:\\Apicture\\" + picUrl));
jLabel9.setIcon(new ImageIcon("C:\\Apicture\\nojpegs.jpg"));
jLabel9.setText("NO Jpeg");
}
jLabel9.setIcon(new ImageIcon(completeurl));
}
Adaption to read from url
URL url;
url = new URL("http://ebid.s3.amazonaws.com/upload_big/9/1/1/1401018425-17770-385.jpg");
Image image = null;
try {
image = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
javax.imageio.IIOException means your are not getting the image.
So add more code to failover to alternate URL/disk in your catch(IOException ) block.
Read http HEAD request and response code 404 for not found. Http is your friend.
Find out of the resource exists before you GET it.
You may want to check this answer I just provided: I can't download a specific image using java code
Seems like amazon is also checking your user-agent, so you may have to put something like this at the beginning of your code:
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
edit: "at the beginning of your code" really means something like "before creating any URL-related object". I mean, I didn't want to refer to the code you posted, but you whole application.
Use this:
URL url;
url = new URL("http://ebid.s3.amazonaws.com/upload_big/9/1/1/1401018425-17770-385.jpg");
url.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
Image image = null;
try {
image = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}

loadDataWithBaseURL does not load images which are part of html page

I am battling with this issue for a long time, dug around Google and SO but still no luck. Finally, I am out here to get your help, please advise or help.
My problem with the following source code is that it only displays string content the images do not display instead it shows white rectangle or sometimes a blue image with question mark.
Q: How to display images?
Here is my code:
private void openURL() {
DefaultHttpClient client = new HttpsRequest(getApplicationContext());
HttpGet get = new HttpGet(getUrlField().getText().toString());
// Execute the GET call and obtain the response
HttpResponse getResponse;
try {
getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();
String content = EntityUtils.toString(responseEntity);
getWebView().loadDataWithBaseURL(null, content, "text/html", "utf-8",
null);
} catch (ClientProtocolException e) {
WSLog.e(THIS_FILE, "HTTP Error.");
e.printStackTrace();
} catch (IOException e) {
WSLog.e(THIS_FILE, "Url Load Error.");
e.printStackTrace();
}
}
webView output:
With the help of Vasarat and few modification from my side helped me to answer this question. I have modified the following code line as
getWebView().loadDataWithBaseURL("http://mywebSite.com/parent_dir_to_iamges/", content, "text/html", "UTF-8","about:blank");
This modification gave me the perfect output as expected.
Please fallow the comments to understand details about the issue.
Note: I have used http in the base url instead https.....Please let me know if I can use https. webview.Loadurl() with https url works fine if API level is 10 or above but it shows blank page for API level 8.

Categories

Resources