Android url in UTF8 - android

I am trying to download image using an url like:
url --> http://www.example.com/path/to/image-ğüçöşı.jpg
InputStream input = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
However in the first line app crashes. Because it has a character like "ı" or "ç". If url doesn't has those character it doesn't crash and works fine.
I could almost say i tried most of the solutions like utf8 encoding and etc, including giving "UTF8" params to HttpClient.
It would be appreciated very much if you could help me. I am looking for any solution that doesn't slow down the code very much.
Thank you

Encode your url or only image name with this code
String query = URLEncoder.encode("strangeChars", "utf-8");

Related

Correct method to display Bitmap from url on ImageView that works on emulator and physical device?

I am building an Android app for local media and parse the relevant images and such from The Movie Database API, Everything is working fine on the Android Studio emulator but when i build the apk and install on a real device none of the downloaded images are displayed on their ImageViews.
The code i am using is;
String Url = Actionlist1.getString(t);//pasrsed from a JSONArray object
java.net.URL url = new java.net.URL(Url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap b = BitmapFactory.decodeStream(input);
Bitmap b1 = Bitmap.createScaledBitmap(b, 100, 140, false);//hard coded scaling to the ImageView width and height.
ImageView B1 = findViewById(R.id.Button1);
B1.setImageBitmap(b1);
This code works fine when ran on the emulator but when ran on a device all images are black, i'm at a loss as the code should work and other than the image being missing the app behaves correctly.
Try this
Glide.with(this)
.asBitmap()
.load(//url)
.placeholder(R.drawable.vijay)
.into(imageView);
add this dependency also
implementation 'com.github.bumptech.glide:glide:4.9.0'
Just add this line in your app.gradle file
implementation 'com.squareup.picasso:picasso:2.71828'
after that you can easily load your image from online.
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
Going to drop the answer in for any one who faces the same problem. The discrepancy between the network methods used between the Android versions, newer versions will not load image assets that did not come from a secure connection. If your facing the same issue change the URL as below;
//Old URL
String URL= "http://image.tmdb.org/t/p/w500/l4iknLOenijaB85Zyb5SxH1gGz8.jpg";
//New URL
String URL= "https://image.tmdb.org/t/p/w500/l4iknLOenijaB85Zyb5SxH1gGz8.jpg";
the s really does make all the difference.

Android webview automatically decode string

We have send a POST request to android web view , but the web view did some url decode automatically and the server side get a wrong data.
Eg: we have a signature value like aedTH5634+hjsGT78-67ty when we POST this value through webview , webview automatically convert + value to space.SO in the server signature value is wrong.How I Avoid this decode.
IOS webview workig fine it send exact value what we have POST.How we avoid this decoding from the android webview.
Help is highly appreciable,
Thanks,
not sure how it will help others with a similar situation but i will still give my $0.02, I was able to solve my issue like this, I used the following methods and ensured that the encoding is retained for encoding value use base64 and convert the data to base 64, read more about this here
void loadData (String data,
String mimeType,
String encoding)
convert data to base64 like this
String base64Data = android.util.Base64.encodeToString(yourdata.getBytes("UTF-8"), android.util.Base64.DEFAULT);
and finally put everything together like this
webView.loadData(base64Data, "text/html; charset=utf-8", "base64");
Maybe an Android WebView Bug?
Just a workaround:
webView.loadUrl(URLEncoder.encode(yourStr));
code works.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
this.evaluateJavascript(javascriptCommand, null);
} else {
this.loadUrl(javascriptCommand);
}

Display byte array of image in imageview

I am trying to show byte array of image from server inside the image view of an android activity. I am able to get the byte array correctly as sent from the server, but while converting it into bitmap it is always returning null. I have used the BitmapFactory.decode(byteArray,0, byteArray.length) for converting image byte array to bitmap which is returning null always.
Please help me solving this and tell me if there are any alternative.
Thanks in advance.
Well a better option is to use this. I used it to display images that is present on a server. Pretty fast
URL url = new URL(link);
InputStream picin = url.openStream();
Bitmap myBitmap = BitmapFactory.decodeStream(picin);
pic.setImageBitmap(myBitmap);
Where link points to the jpg image. Works for other image formats also.

Unable to download file from FTP server using URLConnection having spaces in path

I'm Unable to download file from FTP server using URLConnection having spaces in pat
String s = "ftp://username:password#ftpclient:21/AAB BBC/hhhh 0001.jpg";
URL u = new URL(s);
URLConnection uc = u.openConnection();
BufferedOutputStream bos = new BufferedOutputStream(uc.getOutputStream());
Dont want FTP client solution.
Using URLencoder getting 550 error file not found.
Thanks,
Gaurav
Are you using Apache commons Library ? If so , use this code
try {
FileOutputStream desFileStream = new FileOutputStream(desFilePath);;
status = FTP_object.retrieveFile(srcFilePath, desFileStream);
desFileStream.close();
return status;
} catch (Exception e) {
Log.d(TAG, "download failed");
}
You shouldn't have spaces in your URL itself per RFC1738 section 2.2 Run it through the URL Encode method to encode it:
String s = "ftp://username:password#ftpclient:21/AAB BBC/hhhh 0001.jpg";
String encodedUrl = URLEncoder.encode(s,"UTF-8");
I realize you said you were already doing the encoding, but that returned a 550 error. I didn't see the encoding so am just mentioning it should be needed.
I would really try this from a browser and see if you can get to it. I would also dump out the URL it is using and try that from a browser (or wget, curl, whatever you have handy). The 550 is listed as being a "permission" problem, not file not found, so I'm a little surprised at that, but that may be the short code and come up as an error to prevent people from poking around testing user/password combinations. Hard to say.
The other question I have for you is that you mention you don't want a client solution, but you seem to be writing a client not a server. You're going to port 21, which is the default FTP port for a server.
I would try various combinations of the encoding and see if maybe you're not encoding everything...you should encode the url path. Does the password have any funky characters in it?
Testing from the browser directly will give you a lot of insight.

Getting profile pic from Facebook and display it in Android App

I'm trying to get a users facebook profile pic using the FB api.
My way of doing this isn't working... first I make a request like this:
String str = facebook.request("me/picture");
Next I create an inputstream from the string str so I can decode it with a bitmapfactory
InputStream is = new ByteArrayInputStream(str.getBytes());
Bitmap bm = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bm);
This never displays the image!
Haven't been able to find much about this. Any help would be appreciated.
I fixed this by just using HTTPGet to the url The first time I tried it I got a 404 error but when I rewrote it it worked!

Categories

Resources