How to call value from string folder in android? - android

How do i call a link that is stored under
R.strings.myLink , in the below line
URL url = new URL("https://www.myLink.com");

getString(R.string.myLink)
If above shows error, then use below:
context.getResources().getString(R.string.myLink)

use this code
try {
URL url = new URL(getResources().getString(R.string.urllink));
} catch (MalformedURLException e) {
e.printStackTrace();
}

Related

How to use getClassLoader() correctly?

Here I try to use getClassLoader().getResources() to get my .model file, however it returns null. I'm not sure where goes wrong!
And when I try to print out the urls, it gives me java.lang.TwoEnumerationsInOne#5fd1900, what does this means?
public Activity(MainActivity activity) {
MainActivity activity = new MainActivity();
try {
// Open stream to read trained model from file
InputStream is = null;
// this .model file is save under my /project/app/src/main/res/
Enumeration<URL> urls = Activity.class.getClassLoader().getResources("file.model");
// System.out.println("url:"+urls);
if (urls.hasMoreElements()) {
URL element = urls.nextElement();
is = element.openStream();
}
// deserialize the model
classifier = (J48) SerializationHelper.read(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
In Android, resources put under src/main/res are not visible in the class path and can only be accessed via the android resources API. Try to put the file into src/main/resources.

Android Get XML from URL as string

I am very new to Android Development and Java,
I have C# experience
anyway
I am trying to get XML from a URL
I tried every possible thing but non is working.
Always get crash or no result.
I just want a simple XML result, I found some JSON and PHP code, that i do not want to connect to MySQL.
is there any simple function to retrieve the XML as string from a URL
this is one of my traials
`
lbl1 = (TextView) findViewById(R.id.lbl1);
try {
InputStream is = new URL("http://api.androidhive.info/pizza/?format=xml").openConnection().getInputStream();
String strxml = is.toString();
lbl1.setText(strxml);
}
catch (MalformedURLException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}`
any help will be much appreciated. Thanks
How would you do it in C#?
URL.toString() and (in c#) Request.Url.ToString() are alike. Both behave the same way: creating a string of the URL.
(Like in C#) you would need a HTTP client: http://android-developers.blogspot.de/2011/09/androids-http-clients.html

How to get facebook profile picture of user in facebook SDK 3.0 Android

i am using facebook SDK 3.0 i have to get profile picture of user login.
Here is the code I use:
URL image_value = new URL("http://graph.facebook.com/"+id+"/picture" );
profPict=BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
But I don't get the desired result.
You should change your code following:
URL image_value = new URL("http://graph.facebook.com/"+id+"/picture" );
Possible GET parameters for the URL can be found here:
https://developers.facebook.com/docs/graph-api/reference/user/picture/
Use https:// instead of http:// i faced the same problem.
URL image_value = new URL("https://graph.facebook.com/"+id+"/picture" );
profPict = BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
If you are trying to display profile pic in your app, use ProfilePictureView from Facebook SDK.
Refer This
Just call setProfileId(String profileId) on it.
It will take care of displaying the image.
String id = user.getId();
try {
URL url = new URL("http://graph.facebook.com/"+ id+ "/picture?type=large");
String image_path = uri.toString();
System.out.println("image::> " + image_path);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
Use ProfilePictureView from facebook sdk.
You can do something like this inside a thread:
String url = "http://graph.facebook.com/"+id+"/picture";
HttpConnection conn = new HttpConnection(url);
conn.openConnection();
Drawable d = Drawable.createFromStream(new BufferedInputStream(conn.getInputStream()), "image");
conn.close();
I hope it help you.
try this..
try {
imageURL = new URL("https://graph.facebook.com/" +
id+ "/picture?type=large");
Log.e("URL", imageURL.toString());
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
try {
bitmap = BitmapFactory.decodeStream(imageURL
.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ProfileDp.setImageBitmap(bitmap);
Once your facebook account is logged into the application, simply do:
String url = Profile.getCurrentProfile().getProfilePictureUri(x, y).toString();
x and y are width and height.
See the doc : https://developers.facebook.com/docs/reference/android/current/class/Profile/

Write File in Java Android, Read the File with Phonegap

I'm trying to write a json file using android like this:
String jsonString = broadcastDataArray.toString();
Writer output = null;
File fileAnnouncement = new File("announcement.json");
try {
output = new BufferedWriter(new FileWriter(fileAnnouncement));
output.write(jsonString);
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and now i'm trying to load/read the file using phonegap. Using this method shown here, i am able to read a file that was included in my application source. But i want to read some file that was generated by the android code.
Is there anyway to do this? Is there any specific directory where the file could be accessed from Java android and Phonegap/Sencha ?
Any kind of help or pointer is appreciated. Thanks.
You can do this in following way.
Write this function in your Android activity to write data in text file.
public void WriteDataToFile(String sFileName, String sBody){
try
{
FileWriter f = new FileWriter("/sdcard/data.txt");
f.append(sBody);
f.flush();
f.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
And, now you can read that file from your Sencha/Phonegap app by calling this function.
var user_data = function(){
var request = new XMLHttpRequest();
request.open("GET", "/sdcard/data.txt");
request.onreadystatechange = function() {
if (request.readyState == 4) {
// alert("*" + request.responseText + "*");
data = request.responseText;
}
}
request.send();
}

Get Meta Data of Image - Android

http://www.isco.com/webproductimages/appBnr/bnr1.jpg
I've used a website to see the metadata of a image. In this website, it shows all info of image. I want to know how to get the "Title" tag of above image in android.
I found here the similiar question for iOS only: How to get image metadata in ios
However I don't know how to get "Meta data" of image on android. ExifInterface only gives some informations. But I'm unable to get "Title" tag with it.
Can you provide any code snippet for get meta data in android for image?
Download metadata extractor from the link given here ...... click to download the library
choose the version 2.5.0.RC-3.zip
Extract the jar Folder
and import jar into libs folder in your poject and then execute the below code
try {
InputStream is = new URL("your image url").openStream();
BufferedInputStream bis = new BufferedInputStream(is);
Metadata metadata = ImageMetadataReader.readMetadata(bis,true);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.println(tag);
}
}
}
catch (ImageProcessingException e){}
catch (IOException e) {}
If you want to retrieve meta data information about an image ExifInterface is what you are looking for. Here is a quite good example of how this interface is used: http://android-er.blogspot.com/2009/12/read-exif-information-in-jpeg-file.html
But if you want to retrieve information of an online image I'm afraid it's not yet possible.
If you want to retrieve metadata from image in Android project, then you can do that with the help of: https://github.com/drewnoakes/metadata-extractor
Implement this in your gradle using
implementation 'com.drewnoakes:metadata-extractor:2.12.0'
Complete code is as follows
private static String getImageMetaData(Uri image1) {
try {
InputStream inputStream = FILE_CONTEXT.getContentResolver().openInputStream(image1);
try {
image1.getEncodedUserInfo();
Metadata metadata = ImageMetadataReader.readMetadata(inputStream);
for (Directory directory1 : metadata.getDirectories()) {
if (directory1.getName().equals("Exif IFD0")) {
for (Tag tag : directory1.getTags()) {
if (tag.getTagName().equals("Date/Time")) {
return tag.getDescription();
}
}
}
}
} catch (ImageProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}

Categories

Resources