My application contains a listview. Each listitem contains out of a imageview and a textview.
For showing the image i need check if the image exists in de application data folder.
My code works in the most situations, but there is 1 situation where the application crashes every time i try.
I use the following code to check if the image exists:
File file = new File(imgHelper.getSaveImageDirectoryThumb1(imageUrl));
if(file.exists()){
//show image
}
Sometimes i get the following error, else the get view gets closed without giving a error..
Is there anyone who have a clue to make sure the application won't crash or close the view?
Well, as you did not give more code or the exact error message, I suppose there is something wrong with the
imgHelper.getSaveImageDirectoryThumb1(imageUrl)
This might return NULL. Apart from checking if file!=null you should also have a look at the code mentioned above.
A try catch should catch any errors you are getting
try{
File file = new File(imgHelper.getSaveImageDirectoryThumb1(imageUrl));
if(file){
//the file is here
}else{
//display error
}
catch(Exception e){
//Error message
}
Related
I would like to make a xml file that will be modified during the execution of the application and i want to keep it after i close it for the next time i open it.
The first problem is that i don't know where do i have to put the file in the package explorer on Eclipse.
If i put the file on res/raw/ folder i could just read the file, but i can't modify.
I'm working with Jdom2.
The file is a score table for a game that will be modified every time the player finish a game.
That's the code i actually have to read the xml file stored on res/raw
try
{
puntf = getResources().openRawResource(R.raw.punt);
} catch (Exception ex)
{
Log.e("Ficheros", "Error");
}
And that's the code i actually have to modify the xml file(with Jdom2). But of course, that is wrong.
public void escritura()
{
try
{
xmlOutput.output(puntu, new FileOutputStream("punt.xml"));
}
catch (Exception e) {
e.printStackTrace();
}
}
Thanks a lot for your answers.
If you want to save a file and modify it programmatically I would suggest you to store it in this path:
/data/data/com.yourpackage.app/punt.xml
I have never worked with Jdom2, but you can have access to it by adding these lines of code:
File puntFile= new File("data/data/com.yourpackage.app/punt.xml");
FileOutputStream fos = new FileOutputStream(puntFile);
xmlOutput.output(puntu, fos);
You can also see the file in DDMS in file explorer. Just follow that path.
I can't understand if you want to save and edit the file during the process of your application or just save it somewhere before the app starts and edit it afterwords. If so, please give more details about that.
Hope I helped...
You should read uo on storage options on Andriod: THere's an article on this.... Use the resulting input and output streams for JDOM.
I am looking for a solution regarding a repeating log print that is caused by calling
BitmapFactory.decodeFile.
In My app i have a ListView that is being redrawn by a timer every second.
The ListView has an ImageView that gets is image source from the local storage, (not from the network)
The image is stored in :
filePath = /data/data/com.xxx.testlib/files/b22a1a294fd6e5ad3ea3d25b63c4c735.jpg
I am using the following code to redraw the image and it is working fine. with out exception.
try
{
File filePath = context.getFileStreamPath(imageName);
if(filePath.exists()){
bMap = BitmapFactory.decodeFile(filePath.getPath());
}
}catch (Exception e)
{
e.printStackTrace();
}
But when preforming the following line :
bMap = BitmapFactory.decodeFile(filePath.getPath());
I get a print in the log as follow:
03-07 09:55:29.100: I/System.out(32663): Not a DRM File, opening notmally
03-07 09:55:29.105: I/System.out(32663): buffer returned
....
How can i get read from the printing to the log.
Thank you
lior
Edit
Also it lags the phone whenever this operation is performed. And this reduced performance is noticeable specially when the phone is Waked up and we return to activity with this code.
Its more than a year for OP and still no answer is found. If anyone has found solution then please post it.
Thank you.
DRM stands for Digital Rights Management. It's normally a special keys used by owners of content to make sure that your device is authorized to view/play the content. iTunes was notorious for this for ages.
All it's doing is letting you know that the material you are opening is not DRM protected, and therefore can be opened normally.
Hope, this might help you.
I also got the same exception when i tried to save the image captured by camera directly to : /data/data/com.xxx.testlib/images/b22a1a294fd6e5ad3ea3d25b63c4c735.jpg.
Then i first saved the image to default location used by camera and the copied it to : /data/data/com.xxx.testlib/images/b22a1a294fd6e5ad3ea3d25b63c4c735.jpg.
and now "Not a DRM File, opening notmally" is removed from the log and saved the image successfully.
Conclussion : folder :- "/data/data/com.xxx.testlib/" is private and can be accessible from inside the application only.
Maybe it's a permission error.
Do you have added the right permission in your Manifest ?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I'm trying to delete a file that I earlier created in my android app.
The problem I'm having is that the file won't go away. Even though everything seems to work.
I've looked at several post here on stackoverflow, but still not solution. The garbage collections was one of the hints I've found.
System.gc();
System.out.println("Exists: "+file.exists());
System.out.println("Read: "+file.canRead());
System.out.println("Write: "+file.canWrite());
System.out.println("Deleting: " + file);
boolean r = file.delete();
System.out.println("Result of deletion: "+r);
System.gc();
And the result in the log
Exists: true
Read: true
Write: true
Deleting: data/data/no.ntnu.kpro.app/files/kprothales/XOMessage/8
Result of deletion: true
Does anyone have any idea as to why it isn't removed?
EDIT:
Lucifer: Yeah, I have set WRITE_EXTERNAL_STORAGE permission in the manifest.
ShineDown: No, it is just a file without an extension. For now it is containing xml, but this is going to change over time, hence why I have not called it .xml. Could this be a problem?
chintan khetiya: I believe this line is allready included in the code above.
check the answer here:
Android: how to delete internal image file
which is basically suggesting to call deleteFile:
if(activity.deleteFile(imageName))
Log.i(TAG, "Image deleted.");
I am trying to read a CSV file and have it display the contents as a basic list for an Android app. I am using the method given by Kopfgeldjaeger in this thread.
I have added a couple of 'toasts' which either display 'success' or 'fail' at the bottom of the Android screen if the code managed (or didn't manage) to load the CSV file properly. See below:
try {
CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("file.csv")));
for(;;) {
next = reader.readNext();
if(next != null) {
list.add(next);
} else {
break;
}
}
Toast.makeText(getApplicationContext(), "SUCCESS",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "FAIL",
Toast.LENGTH_SHORT).show();
}
When I load the app, I get the 'SUCCESS' message, so all is well so far. Now, I'd like to see if I can load any of the data. In Kopfgeldjaeger's answer, it is suggested that I could access a string using the following code:
list.get(1)[1]
So, in order to check that it's worked, I try to generate another toast, as follows:
Toast.makeText(getApplicationContext(), list.get(1)[1],
Toast.LENGTH_SHORT).show();
This added toast causes the program to fail to load properly. The question is, have I gotten the toast syntax wrong, or is my CSV file not loading properly?
There are a couple of things to check:
Make sure your csv file has a size of at least 2 x 2 entries, otherwise retrieving the data from line index 1 and column index 1 won't work. For example, print or debug list.size() and list.get(0).length to see if they're both at least 2.
Confirm that your csv file is actually comma separated, and not e.g. semicolon separated. I have seen occassions where certain software seems to choose its own delimiter.
As a recommendation: the referenced csv reader is part of ByteCode's OpenCSV. You may want to include the latest source code or jar from that project. It supports custom delimiter characters and also provides a shorthand for parsing all the csv data into a list of string arrays:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
I am using this code to save a bitmap image taken from a screen shot:
try{
String fname = "/Pictures/YourApp/YourPic.jpg";
File path= Environment.getExternalStorageDirectory();
File file = new File(path, fname);
if (file.exists()) {
Bitmap bitty = BitmapFactory.decodeFile(file.toString());
yourPlan.setImageBitmap(bitty); //ImageView
}else{
errorText.setText("If you can not see your image then free up some memory");
}
}catch(Exception e){
errorText.setText("If you can not see your image then free up some memory");
}
It works fine on my testing phone - an Orange San Francisco on 2.1 but I have had emails from users telling me they only get the error message when saving with enough space.
They are using HTC and Samsung handsets. Any ideas?
First, I would like to point out that catching a general Exception e is not in good coding practice. Second, both messages you output to the user are also not helpful as it is not guaranteed that the error is space-related.
I am guessing the error comes from the file not existing. Where are you saving the file? From the snippet of code, I see that you are simply assuming the file exists. If you haven't yet saved the file after taking your screen shot, that would be your problem right there. You are opening a file that doesn't exist and your error message is giving the mistaken impression that there isn't enough space.