Working with Files

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby oladapo » Sat Jan 12, 2008 12:49 am

I have the following error when I try to push a file to the data folder for my project...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. [2008-01-11 18:45:08] Failed to push the items
  2.  
  3. [2008-01-11 18:45:08] null
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


What I am trying to do is figure out how I can display a photo taken from the Camera (like the photo in the CameraCapture tutorial). To get started I was going to put a source picture of something I know.

Anyone been successful in this?
oladapo
Freshman
Freshman
 
Posts: 3
Joined: Mon Jan 07, 2008 6:30 pm
Location: Philadelphia

Top

Postby plusminus » Sat Jan 12, 2008 1:27 am

Hello oladapo,

that was a try from within eclipse, right :?:
:rarrow: Here is :src: your solution. :)


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sasuke » Tue Jan 15, 2008 6:00 am

Hello all,
i got a [font=Courier New]FileNotFoundException[/font] when the program execute this :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. FileOutputStream fOut = openFileOutput("/data/data/dj.android.CustomComponent/files/samplefile.txt",
  3.  
  4.                                      MODE_WORLD_READABLE);
  5.  
  6.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


what's the prob?

Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Tue Jan 15, 2008 11:41 pm

Hello sasuke,

is "dj.android.CustomComponent" the application you are currently developing in / this code is placed :?:

the the following is enough:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileOutputStream fOut = openFileOutput("samplefile.txt", MODE_WORLD_READABLE);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sasuke » Wed Jan 16, 2008 12:56 am

Hello plusminus,

i used prefix "/data/data/dj.android.CustomComponent/files/samplefile.txt" because i have tested with "samplefile.txt" and it's still not working... :roll: :roll:

what's the import? CTRL+SHIFT+O and i got :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.io.FileOutputStream;
  3.  
  4. import java.io.IOException;
  5.  
  6. import java.io.OutputStreamWriter;
  7.  
  8.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Wed Jan 16, 2008 1:07 am

Hello sasuke,

my original imports are:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.FileInputStream;
  2.  
  3. import java.io.FileOutputStream;
  4.  
  5. import java.io.IOException;
  6.  
  7. import java.io.InputStreamReader;
  8.  
  9. import java.io.OutputStreamWriter;
  10.  
  11.  
  12.  
  13. import android.app.Activity;
  14.  
  15. import android.os.Bundle;
  16.  
  17. import android.util.Log;
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Perhaps try another filename :!:

Following is the full source. If it still wont work then you perhaps need a -wipe-data (save important data before :!:) :?:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.workingwithfiles;
  2.  
  3.  
  4.  
  5. import java.io.FileInputStream;
  6.  
  7. import java.io.FileOutputStream;
  8.  
  9. import java.io.IOException;
  10.  
  11. import java.io.InputStreamReader;
  12.  
  13. import java.io.OutputStreamWriter;
  14.  
  15.  
  16.  
  17. import android.app.Activity;
  18.  
  19. import android.os.Bundle;
  20.  
  21. import android.util.Log;
  22.  
  23.  
  24.  
  25. public class WorkingWithFiles extends Activity {
  26.  
  27.     /** Called when the activity is first created. */
  28.  
  29.     @Override
  30.  
  31.     public void onCreate(Bundle icicle) {
  32.  
  33.         super.onCreate(icicle);
  34.  
  35.         setContentView(R.layout.main);
  36.  
  37.        
  38.  
  39.         try {          
  40.  
  41.                         final String TESTSTRING = new String("Hello Android");
  42.  
  43.                        
  44.  
  45.                         // ##### Write a file to the disk #####
  46.  
  47.                         /* We have to use the openFileOutput()-method
  48.  
  49.                          * the ActivityContext provides, to
  50.  
  51.                          * protect your file from others and
  52.  
  53.                          * This is done for security-reasons.
  54.  
  55.                          * We chose MODE_WORLD_READABLE, because
  56.  
  57.                          *  we have nothing to hide in our file */             
  58.  
  59.                         FileOutputStream fOut = openFileOutput("samplefile.txt",
  60.  
  61.                                                                 MODE_WORLD_READABLE);
  62.  
  63.                         OutputStreamWriter osw = new OutputStreamWriter(fOut); 
  64.  
  65.  
  66.  
  67.                         // Write the string to the file
  68.  
  69.                         osw.write(TESTSTRING);
  70.  
  71.                         /* ensure that everything is
  72.  
  73.                          * really written out and close */
  74.  
  75.                         osw.flush();
  76.  
  77.                         osw.close();
  78.  
  79.                        
  80.  
  81.                         // ##### Read the file back in #####
  82.  
  83.                        
  84.  
  85.                         /* We have to use the openFileInput()-method
  86.  
  87.                          * the ActivityContext provides.
  88.  
  89.                          * Again for security reasons with
  90.  
  91.                          * openFileInput(...) */
  92.  
  93.                         FileInputStream fIn = openFileInput("samplefile.txt");
  94.  
  95.                         InputStreamReader isr = new InputStreamReader(fIn);
  96.  
  97.                         /* Prepare a char-Array that will
  98.  
  99.                          * hold the chars we read back in. */
  100.  
  101.                         char[] inputBuffer = new char[TESTSTRING.length()];
  102.  
  103.                         // Fill the Buffer with data from the file
  104.  
  105.                         isr.read(inputBuffer);
  106.  
  107.                         // Transform the chars to a String
  108.  
  109.                         String readString = new String(inputBuffer);
  110.  
  111.                        
  112.  
  113.                         // Check if we read back the same chars that we had written out
  114.  
  115.                         boolean isTheSame = TESTSTRING.equals(readString);
  116.  
  117.                         // Celebrate =)
  118.  
  119.                        
  120.  
  121.                         Log.i("File Reading stuff", "success = " + isTheSame);
  122.  
  123.                 } catch (IOException ioe) {
  124.  
  125.                         ioe.printStackTrace();
  126.  
  127.                 }
  128.  
  129.     }
  130.  
  131. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby sasuke » Wed Jan 16, 2008 1:45 am

Hello plusminus,

it's works now.. after added the -wipe-data.

How to create the file not in our apps path (/data/data/package/files/) ?

Because as you say
plusminus wrote:he application runs with application privileges. I.e. the data folder requires system privileges. When you start adb you run as root. But this application runs only with application-privileges


The file browser won't able to retreive the file in 'data' folder.

Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby cabernet1976 » Wed Jan 16, 2008 6:12 pm

I met a problem when I want to download a bitmap from web then write to SD card, it is failed, but write to /data/data/... with this tutorial is OK. The following is my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private void testDownload(String urlStr) {
  2.         URL url = null;
  3.                 try {
  4.                         url = new URL(urlStr);
  5.                 } catch (MalformedURLException e) {
  6.                         Log.e(TAG, "error1:"+e.toString());
  7.                 }
  8.                
  9.         URLConnection conn = null;
  10.                 try {
  11.                         conn = url.openConnection();
  12.                 } catch (IOException e) {
  13.                         Log.e(TAG, "error2:"+e.toString());
  14.                 }
  15.                
  16.         try {
  17.                         conn.connect();
  18.                 } catch (IOException e) {
  19.                         Log.e(TAG, "error2:"+e.toString());
  20.                 }
  21.                
  22.         InputStream is = null;
  23.                 try {
  24.                         is = conn.getInputStream();
  25.                 } catch (IOException e) {
  26.                         Log.e(TAG, "error3:"+e.toString());
  27.                 }
  28.                
  29.         BufferedInputStream bis = new BufferedInputStream(is);
  30.         Bitmap bm = BitmapFactory.decodeStream(bis);
  31.         File f = new File("/sdcard/1.jpg");
  32.        
  33.         FileOutputStream fos = null;
  34.                 try {
  35.                         fos = new FileOutputStream(f);  // <--error here
  36.                 } catch (FileNotFoundException e) {
  37.                         Log.e(TAG, "error4:"+e.toString());
  38.                 }
  39.                
  40.         bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  41.        
  42.         try {
  43.                         bis.close();
  44.                 } catch (IOException e) {
  45.                         Log.e(TAG, "error5:"+e.toString());
  46.                 }
  47.                
  48.         try {
  49.                         is.close();
  50.                 } catch (IOException e) {
  51.                         Log.e(TAG, "error6:"+e.toString());
  52.                 }
  53.     }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


After runing this method with a valid url, I got the error information:
error4:java.io.FileNotFoundException: /sdcard/1.jpg

I am sure the sdcard.img is fine, I can find a dir named "sdcard" in DDMS, and I have added '-sdcard D:sdcard.img' in Emulator.
Could someone help me out?
Thanks.

-----added on Jan 22, 2008-----
Hi plusminus,
I read your reply just now. My code works fine after set the correct configuration of sdcard path.
Thanks.
Last edited by cabernet1976 on Tue Jan 22, 2008 9:17 am, edited 1 time in total.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby plusminus » Wed Jan 16, 2008 11:38 pm

Hello cabernet1976,

did you give a try to
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileWriter f = new FileWriter("/sdcard/1.jpg");
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Just try to write some chars to it.

Sorry, but no time for actual testing on my own :( (damned exams)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby cabernet1976 » Thu Jan 17, 2008 2:23 am

Hello plusminus,

Thank you to reply me in your busy time.
I got your meaning, I will try to test whether SD Card is able to be wroten by FileWriter.

Thank you again.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby cabernet1976 » Thu Jan 17, 2008 1:15 pm

I make a big mistake :oops:

The following is my lesson:
1. I created a sdcard, and put the sdcard.img in D:dir1sdcard.img
2. But I configured an invalid information to Eclipse Emulator as: -sdcard D:sdcard.img. It is the origin of this issue.
 I did the above two steps and went to bed the day before yesterday.

3. Yesterday, I opened my Android project, but found there is no 'sdcard' in DDMS file explore, but I didn't think it deeply.
4. I run 'adb push C:test1.jpg /sdcard/test1.jpg', and checked DDMS file explore finding there was a sdcard directory now, and test1.jpg was in it. :roll: I think it maybe a bug of adb push, it deceive me.

 Now you know the following story, I got FileNotFoundException, and it took me more than one hour.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby plusminus » Fri Jan 18, 2008 7:06 pm

Hello cabernet1976,

so you are fine now :?:
By pushing that file probably a real folder was created, and not the (pseudo) sd-card.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Re: read file

Postby tsdt » Sat Jan 19, 2008 6:12 am

igal wrote:When I write and read file in the same activity everything is OK.
But when I try to read file from another activity I get "File not found" error.
I do use MODE_WORLD_READABLE option when I write the file, and I do see the file:
"/data/data/com.myname.android/files/JScript.htm"
I use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileInputStream fIn = openFileInput("/data/data/com.myname.android/files/JScript.htm")
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

statement.
Help, please, to find mistake.
Thanks in advance.


I have the same problem too, Igal !!!.Hic,it takes me one day,but no thing resolved(FileNotFound Exception),even i used openFileInput("fileName"); and used "/data/data/.../files/data.txt".Please help me and Igal,i am in hurry for my demo of a apprentice.
TSDT
tsdt
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jan 09, 2008 4:20 am
Location: VIETNAM

Re: Working with Files

Postby krystox » Tue Jan 22, 2008 2:50 am

Hi,

Can someone help me with those problems I have?

How can I create a folder and write files into the folder?
For example: I want to create a file like /data/data/myapp/files/folder1/test.txt

I tried to use File class which has a mkdir() method. It can create a folder inside /tmp, but not other places, nor /data/data/myapp/files/

And I also tried to use openFileOutput("folder1/text.txt"), it returns FileNotFound exception as it can't include a file separator.

Anyone has an idea about this?
krystox
Freshman
Freshman
 
Posts: 7
Joined: Tue Jan 22, 2008 2:39 am

Postby cabernet1976 » Tue Jan 22, 2008 9:26 am

Hi krystox,

I met the same issus ( create_directory_failed_in_file_system-t573.html ), I did it according to a thread in google group but failed.
The thread is http://groups.google.com/group/android- ... b5bd03a2cd , it is said that somebody has done it successfully.
Hope you can make it work.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 11 guests