Solved

Issue creating a file using Java

Posted on 2012-03-22
12
688 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
LVL 8

Expert Comment

by:Santhana
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:Santhana
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
If you use the Google Now Launcher, as an aftermarket add on, have a Samsung Note 5 and are worried about power consumption be wary of using the ultra power saving mode.  Here is what happened to me when I made the mistake of trying this out...
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
Suggested Courses

624 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