I am new to java.
I have a directory with a txt file (R.raw). I want to get access with a command
InputStream in_s = res.openRawResource(R.raw.itemname);
where itemname is a dynamic string with a filename from a previous activity.
How do I can get open file in R.raw by a string "n0.txt"?
In javascript I can implement it like R.raw["n0.txt"] or R.raw.itemname.
Thanks in advance.
You need to use method getIdentifier().
Context context;
Resources res;
int itemId = res.getIdentifier("itemname", "raw", context.getPackageName());
InputStream is = res.openRawResource(itemId);
Original post here.
Related
I need a quick help on how to convert android Uri to Java URI. My requirement is to capture images and store them in External storage. To pass these images across activities, I decided to use an Arraylist that holds Uris of images and pass on this arraylist as an intent-extra to next activity. But, Arraylist accepts only JavaURI.
String image = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
File photo=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),image);
selectedImageA=Uri.fromFile(photo);
imageIndex++;
selectedImageJ= <Looking for code here>;
imagesUriArray.add(imageIndex,selectedImageJ);
Intent i = new Intent(getApplicationContext(),ScanActivity.class);
startActivity(i);
Try to declare:
ArrayList<Uri> imagesUriArray = new ArrayList<Uri>;
If this doesn't work, declare your ArrayList as String list and convert the uri to String. And later, initialize the uri from the Uri string
I want to add a help screen to my Codename One App.
As the text is longer as other strings, I would like put it in a separate file and add it to the app-package.
How do I do this? Where do I put the text file, and how can I easily read it in one go into a string?
(I already know how to put the string into a text area inside a form)
In the Codename One Designer go to the data section and add a file.
You can just add the text there and fetch it using myResFile.getData("name");.
You can also store the file within the src directory and get it using Display.getInstance().getResourceAsStream("/filename.txt");
I prefer to have the text file in the filesystem instead of the resource editor, because I can just edit the text with the IDE. The method getResourceAsStream is the first part of the solution. The second part is to load the text in one go. There was no support for this in J2ME, you needed to read, handle buffers etc. yourself. Fortunately there is a utility method in codename one. So my working method now looks like this:
final String HelpTextFile = "/helptext.txt";
...
InputStream in = Display.getInstance().getResourceAsStream(
Form.class, HelpTextFile);
if (in != null){
try {
text = com.codename1.io.Util.readToString(in);
in.close();
} catch (IOException ex) {
System.out.println(ex);
text = "Read Error";
}
}
The following code worked for me.
//Gets a file system storage instance
FileSystemStorage inst = FileSystemStorage.getInstance();
//Gets CN1 home`
final String homePath = inst.getAppHomePath();
final char sep = inst.getFileSystemSeparator();
// Getting input stream of the file
InputStream is = inst.openInputStream(homePath + sep + "MyText.txt");
// CN1 Util class, readInputStream() returns byte array
byte[] b = Util.readInputStream(is);
String myString = new String(b);
I have a question
private final String images[] = {"http://developer.android.com/images/dialog_custom.png", "http://developer.android.com/images/dialog_progress_bar.png"};
how can i change this
to load all of the links from a file from my website?
like a .txt on my site just has like
http://link.com/1.png
http://link.com/2.png
http://link.com/3.png
http://link.com/4.png
http://link.com/5.png
http://link.com/6.png
http://link.com/7.png
http://link.com/8.png
http://link.com/9.png
http://link.com/10.png
so it will just load all those then later on i can add more links without having to update the app
Use a BufferedReader to readLine() to a String and then fetch the image...
Just put the space or "," or "/n" in file then get all data in a string one by one
String reader = "";
String separated ;
BufferedReader buffer = new BufferedReader(new FileReader("YourFileName.txt"));
while ((reader = buffer.readLine()) != null) {
separated = reader.split(" "); or reader.split(","); or reader.split("/n");
//Here at each itration separated will have your single link
}
buffer.close;
Try out the following links:
How to load an ImageView by URL in Android?
http://russenreaktor.wordpress.com/2009/08/11/android-imageloader-load-images-sequencially-in-the-background/
There are plenty of links that provide solutions--enjoy.
Is it possible to get the name of an image in java. The image source is a url.
For eg. "http://172.16.2.42/apache_pb.png"
I need the output "apache_pb.png"
String s = "http://172.16.2.42/apache_pb.png";
int index = s.lastIndexOf('/');
String name = s.substring(index+1);
System.out.println(name);
You can use this helper method from the URL class:
String file = new URL("http://172.16.2.42/apache_pb.png").getPath();
Would URL#getFile() do this for you?
I'm trying to generate a PDF document using FOP. The pdf generation code is kept in a servlet and the xsl is in a specific folder in the WebContent folder.
How can I access this xsl file by giving a relative path? It works only if I give the complete path in the File object.
I need to generate the xml content dynamically. How can I give this dynamically generated xml as the source instead of a File object?
Please provide your suggestions.
To get the path you can just do:
String path = s.getServletContext().getRealPath("/WEB-INF/somedir/hdfeeh");
s is the class that implements HTTPServlet.You can also use this.getServletContext() if its your servlet class.
Then pass this as a parameter.
As far as using dynamically generated XML, the library you're using should support using an input stream, write your XML, convert it to a byte array, then wrap it in a ByteArrayInputStream and use this.
For a direct and independent container implementation, you can access the resourcewith the following method getResource() inside your servlet:
/start servlet/
public InputStream getResource(String resourcePath) {
ServletContext servletContext = getServletContext();
InputStream openStream = servletContext.getResourceAsStream( resourcePath );
return openStream;
}
public void testConsume() {
String path = "WEB-INF/teste.log";
InputStream openStream = getResource( path );
int c = -1;
byte[] bb = new byte[1024];
while ( -1 != ( c = openStream.read( bb ) ) ) {
/* consume stream */
}
openStream.close();
}
/end servlet/
I used the following method to read the file under web content
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getSession().getServletContext().getResourceAsStream("/json/sampleJson.json")));
Now all the file content is available in the reader object.