Link to home
Start Free TrialLog in
Avatar of Sreejith22
Sreejith22Flag for India

asked on

Issue creating a file using Java

File not found exception is obtained with the following code. I get this only once, on first execution. After that, I do not get this exception at all. Even while I get this exception for the first time, I get the file "ico" created with the specified path. Why does I get this during first time and not after that? How can I resolve this issue?

try {
System.out.println("Icon file :" + iconFile);
File ico = new File(iconFile); //creating a file with the path (because only this is working with absolute path) 
ico.setReadable(true, false);
System.out.println(" Icon path : "+ico);
fis = new FileInputStream(ico);
System.out.println("Iconcache fileinputstream" +fis);
d = new BitmapDrawable(mContext.getResources(),fis);
System.out.println("Iconcache d " + d);
System.out.println("Iconcache getFullResIcon after fileinputstream  ");
fis.close();
}
catch (OutOfMemoryError e) 
{
System.out.println(" outmemory");
e.printStackTrace();
} catch (FileNotFoundException e) {
System.out.println(" filenotfound");
// TODO Auto-generated catch block
e.printStackTrace();
} 

Open in new window



Exception:

437: INFO/System.out(572): Icon file :/data/data/com.android.settings/app_themes/browser.png
01-01 00:04:43.812: INFO/System.out(572):  Icon path : /data/data/com.android.settings/app_themes/browser.png
01-01 00:04:43.812: INFO/System.out(572):  filenotfound

Open in new window


Any help in this regard, which would help me resolve this problem, is much appreciated.

Regards,
Sree
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

on which line IOException (filenotfound) is coming?
it looks like iconFile path may not be valid or accessible.

did you checked this?
http://stackoverflow.com/questions/9049601/saving-bitmap-io-exception
Avatar of Sreejith22

ASKER

fis = new FileInputStream(ico);
then i guess, either your path is not right in the sense that 'file doesn't not exists as of now'

please check here
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FileInputStream.html

it says that your file need to exist for this API to work

use this example, to check if the file exists or not
http://www.roseindia.net/java/example/java/io/CreateFile.shtml
CODE:

File ico = new File(iconFile); 
ico.setReadable(true, false);
System.out.println("FileExecute " + ico.canExecute());
System.out.println("FileRead " + ico.canRead());
System.out.println("FileWrite " + ico.canWrite());
System.out.println("FileExists " + ico.exists());
System.out.println("FileisFile " + ico.isFile());
System.out.println("FileisDirectory " + ico.isDirectory());

Open in new window



First execution:

FileExecute false
FileRead false
FileWrite false
FileExists true
FileisFile true

Open in new window


Second execution:

FileExecute false
FileRead true
FileWrite false
FileExists true
FileisFile true

Open in new window



As you can see,  on first execution, FileRead permission is false. I presume, this might be causing the issue. How can I resolve this, if it is the real cause?
HI Sreejith22,
I too find some issues related to this setReadable() and setWritable()......

In some versions of JDK have issues in changing the permission....
http://stackoverflow.com/questions/5302269/java-file-setwritable-and-stopped-working-correctly-after-jdk-6u18
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6368018

I have tried the same example by accessing a icon file from the same path...
I tried by manually change the permission  of the file and it was working perfectly to me......
But when i programatically change the permission by using setReadable(true)...It is always returning false to me.....
But why this happens only once is what puzzles me. Also, did you have a solution for that santhanasamy?
May be for the first time its taking time to read the file. The second time its ready to be read. Is it the First execution after compilation. Are you compiling again for the second try.
HI Sreejith22,

I found this nice article by Venabili in EE.Here he summarized  java permission related concepts.....
https://www.experts-exchange.com/Programming/Languages/Java/A_1498-File-permissions-with-Java.html


From the android DOcs

public boolean setReadable (boolean readable, boolean ownerOnly) Since: API Level 9 Manipulates the read permissions for the abstract path designated by this file.
Returns
true if and only if the operation succeeded. If the user does not have permission to change the access permissions of this abstract pathname the operation will fail. If the underlying file system does not support read permission and the value of readable is false, this operation will fail.

This is what i am gettiing from these....
So If the user does not have permission to change the access permissions of this abstract pathname ,it will not allow to change the permission  even through programatically......

I am not facing the senario like you are facing....
For me programatically not able to set the permission to "other if i set remove the read permission manually....
ASKER CERTIFIED SOLUTION
Avatar of Sreejith22
Sreejith22
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NO solution yet