Solved

Issue creating a file using Java

Posted on 2012-03-22
12
649 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
Comment Utility
on which line IOException (filenotfound) is coming?
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
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
Comment Utility
fis = new FileInputStream(ico);
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
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
Comment Utility
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
Comment Utility
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No exact solution yet for the issue.
0
 

Author Closing Comment

by:Sreejith22
Comment Utility
NO solution yet
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

771 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

10 Experts available now in Live!

Get 1:1 Help Now