Solved

Issue creating a file using Java

Posted on 2012-03-22
12
658 Views
Last Modified: 2012-04-01
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
0
Comment
Question by:Sreejith22
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37751926
on which line IOException (filenotfound) is coming?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37751934
it looks like iconFile path may not be valid or accessible.

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

Author Comment

by:Sreejith22
ID: 37751936
fis = new FileInputStream(ico);
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37752012
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
0
 

Author Comment

by:Sreejith22
ID: 37752042
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?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37752221
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 8

Expert Comment

by:santhanasamy
ID: 37752326
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.....
0
 

Author Comment

by:Sreejith22
ID: 37752384
But why this happens only once is what puzzles me. Also, did you have a solution for that santhanasamy?
0
 
LVL 1

Expert Comment

by:haravallabhan
ID: 37752566
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.
0
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37752576
HI Sreejith22,

I found this nice article by Venabili in EE.Here he summarized  java permission related concepts.....
http://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....
0
 

Accepted Solution

by:
Sreejith22 earned 0 total points
ID: 37770763
No exact solution yet for the issue.
0
 

Author Closing Comment

by:Sreejith22
ID: 37792602
NO solution yet
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now