Team,
I would like to know, whether we can add custom configurations in Manifest or deploy properties? If yes, please provide examples.
thanks,
Ramesh
Put your .properties file into the /res/raw directory
try {
Resources resources = this.getResources();
// Check generated R file for name oy your resource
InputStream rawResource = resources.openRawResource(R.raw.resourceFileName);
Properties properties = new Properties();
properties.load(rawResource);
System.out.println("The properties are now loaded");
System.out.println("properties: " + properties);
} catch (NotFoundException e) {
System.err.println("Did not find raw resource: "+e);
} catch (IOException e) {
System.err.println("Failed to open property file");
}
Source: http://myossdevblog.blogspot.com/2010/02/reading-properties-files-on-android.html
Related
I've created a folder 'test' inside res and I want to display them in an image view. How exactly can I fetch the image with a given name in the designated folder?
ImageView test = (ImageView) testing.findViewById(R.id.test);
flag.setImageDrawable(getResources().); <==== This?
EDIT
InputStream is = null;
try {
is = this.getResources().getAssets().open("country_flags/sample.png");
} catch (IOException e) {
;
}
image = (ImageDrawable) BitmapFactory.decodeStream(is);
try {
flag.setImageDrawable(getResources().getAssets().open("country_flags/"+nationality+".png"));
} catch (IOException e) {
e.printStackTrace();
}
How exactly can I fetch the image with a given name in the designated folder?
You don't. You cannot invent new resource types, and so your test directory will, at best, be forever ignored.
I know how to delete a file, it's:
File file = new File(path);
file.delete();
Can I test that the file is currenlty in use without rooting my device?
For exmple I want to check if the file is open before i can delete it.
I want to be able to catch errors with a try catch sentence.
You can use a try/catch clause.
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
You can find more information here.
I´m creating a game for my college project and I was wondering if I can get the names(just the names) of the images I put inside the /drawable folder in android. It can also be the files I put inside the /raw folder, the option that works better.
Use the following snippet
Field[] ID_Fields = R.drawable.class.getFields();
for (Field f : ID_Fields) {
try {
System.out.println(f.getName() + "sample");
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
I am using AsyncTask to download a file from a web folder. However, the name of the file will be different every time.
Is there a way to get the name of the file in a web folder? (There will be only 1 file in that folder at a given time).
http://www.example.com/myfolder/myfile_1
Try the ApacheURLLister (I didn't tested it but it may work):
URL url;
List serverDir = null;
try {
url = new URL("http://www.abc.com/myfolder/");
ApacheURLLister lister = new ApacheURLLister();
serverDir = lister.listAll(url);
}
catch (Exception e) {
e.printStackTrace();
Log.e("ERROR ON GETTING FILE","Error is " +e);
}
System.out.println(serverDir);
return serverDir;
As described in android documentation, to create a new file which will be situated in the application's directory there is the method in Context class: openFileOutput().
But where will be the file situated if I use simple createNewFile() method from File class.?
CreateNewFile() is used like this:
File file = new File("data/data/your package name/test.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
So you will tell the file where it should be created. Remember that you can only create new files on your package. That is "data/data/your package name/".
Somehow createNewFile() was not able to create the complete file path here on my devices.
try {
if (!futurePhotoFile.exists()) {
new File(futurePhotoFile.getParent()).mkdirs();
futurePhotoFile.createNewFile();
}
} catch (IOException e) {
Log.e("", "Could not create file.", e);
Crouton.showText(TaskDetailsActivity.this,
R.string.msgErrorNoSdCardAvailable, Style.ALERT);
return;
}
it will be stored in the current directory to which your classPath is pointing to
Depends on the path you pass to the File constructor. If the parent directory exists, and if you have the permission to write to it, of course.
Documentation of createNewFile() method says:
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.
Therefore we don't need to check existence of a file manually:
val dir = context.filesDir.absolutePath + "/someFolder/"
val logFile = File(dir + "log.txt")
try {
File(dir).mkdirs() // make sure to call mkdirs() when creating new directory
logFile.createNewFile()
} catch (e: Exception) {
e.printStackTrace()
}