andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

Working with Files

Goto page Previous  1, 2, 3, 4, 5  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
oladapo
Freshman
Freshman


Joined: 07 Jan 2008
Posts: 3
Location: Philadelphia

PostPosted: Sat Jan 12, 2008 12:49 am    Post subject: Reply with quote

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

Java:
[2008-01-11 18:45:08] Failed to push the items
[2008-01-11 18:45:08] null


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?
Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2660
Location: College Park, MD

PostPosted: Sat Jan 12, 2008 1:27 am    Post subject: Reply with quote

Hello oladapo,

that was a try from within eclipse, right Question
Arrow Right Here is Source your solution. Smile


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sasuke
Experienced Developer
Experienced Developer


Joined: 30 Nov 2007
Posts: 57

PostPosted: Tue Jan 15, 2008 6:00 am    Post subject: Reply with quote

Hello all,
i got a FileNotFoundException when the program execute this :
Java:

FileOutputStream fOut = openFileOutput("/data/data/dj.android.CustomComponent/files/samplefile.txt",
                                     MODE_WORLD_READABLE);


what's the prob?

Regards,
sasuke
Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2660
Location: College Park, MD

PostPosted: Tue Jan 15, 2008 11:41 pm    Post subject: Reply with quote

Hello sasuke,

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

the the following is enough:

Java:
FileOutputStream fOut = openFileOutput("samplefile.txt", MODE_WORLD_READABLE);


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sasuke
Experienced Developer
Experienced Developer


Joined: 30 Nov 2007
Posts: 57

PostPosted: Wed Jan 16, 2008 12:56 am    Post subject: Reply with quote

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... Rolling Eyes Rolling Eyes

what's the import? CTRL+SHIFT+O and i got :
Java:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;


Regards,
sasuke
Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2660
Location: College Park, MD

PostPosted: Wed Jan 16, 2008 1:07 am    Post subject: Reply with quote

Hello sasuke,

my original imports are:
Java:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


Perhaps try another filename Exclamation

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

Java:
package org.anddev.android.workingwithfiles;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class WorkingWithFiles extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
       
        try {           
               final String TESTSTRING = new String("Hello Android");
               
               // ##### Write a file to the disk #####
               /* We have to use the openFileOutput()-method
                * the ActivityContext provides, to
                * protect your file from others and
                * This is done for security-reasons.
                * We chose MODE_WORLD_READABLE, because
                *  we have nothing to hide in our file */
        
               FileOutputStream fOut = openFileOutput("samplefile.txt",
                                        MODE_WORLD_READABLE);
               OutputStreamWriter osw = new OutputStreamWriter(fOut)

               // Write the string to the file
               osw.write(TESTSTRING);
               /* ensure that everything is
                * really written out and close */

               osw.flush();
               osw.close();
               
               // ##### Read the file back in #####
               
               /* We have to use the openFileInput()-method
                * the ActivityContext provides.
                * Again for security reasons with
                * openFileInput(...) */

               FileInputStream fIn = openFileInput("samplefile.txt");
               InputStreamReader isr = new InputStreamReader(fIn);
               /* Prepare a char-Array that will
                * hold the chars we read back in. */

               char[] inputBuffer = new char[TESTSTRING.length()];
               // Fill the Buffer with data from the file
               isr.read(inputBuffer);
               // Transform the chars to a String
               String readString = new String(inputBuffer);
               
               // Check if we read back the same chars that we had written out
               boolean isTheSame = TESTSTRING.equals(readString);
               // Celebrate =)
               
               Log.i("File Reading stuff", "success = " + isTheSame);
          } catch (IOException ioe) {
               ioe.printStackTrace();
          }
    }
}

Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sasuke
Experienced Developer
Experienced Developer


Joined: 30 Nov 2007
Posts: 57

PostPosted: Wed Jan 16, 2008 1:45 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
cabernet1976
Senior Developer
Senior Developer


Joined: 16 Nov 2007
Posts: 154
Location: China

PostPosted: Wed Jan 16, 2008 6:12 pm    Post subject: Reply with quote

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:
Java:
   private void testDownload(String urlStr) {
     URL url = null;
          try {
               url = new URL(urlStr);
          } catch (MalformedURLException e) {
               Log.e(TAG, "error1:"+e.toString());
          }
          
        URLConnection conn = null;
          try {
               conn = url.openConnection();
          } catch (IOException e) {
               Log.e(TAG, "error2:"+e.toString());
          }
          
        try {
               conn.connect();
          } catch (IOException e) {
               Log.e(TAG, "error2:"+e.toString());
          }
          
        InputStream is = null;
          try {
               is = conn.getInputStream();
          } catch (IOException e) {
               Log.e(TAG, "error3:"+e.toString());
          }
          
        BufferedInputStream bis = new BufferedInputStream(is);
        Bitmap bm = BitmapFactory.decodeStream(bis);
        File f = new File("/sdcard/1.jpg");
       
        FileOutputStream fos = null;
          try {
               fos = new FileOutputStream(f);  // <--error here
          } catch (FileNotFoundException e) {
               Log.e(TAG, "error4:"+e.toString());
          }
          
        bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
       
        try {
               bis.close();
          } catch (IOException e) {
               Log.e(TAG, "error5:"+e.toString());
          }
          
        try {
               is.close();
          } catch (IOException e) {
               Log.e(TAG, "error6:"+e.toString());
          }
    }


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.

_________________
Upload2Flickr's blog: http://upload2flickr.blogspot.com


Last edited by cabernet1976 on Tue Jan 22, 2008 9:17 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2660
Location: College Park, MD

PostPosted: Wed Jan 16, 2008 11:38 pm    Post subject: Reply with quote

Hello cabernet1976,

did you give a try to
Java:
FileWriter f = new FileWriter("/sdcard/1.jpg");

Just try to write some chars to it.

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

Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
cabernet1976
Senior Developer
Senior Developer


Joined: 16 Nov 2007
Posts: 154
Location: China

PostPosted: Thu Jan 17, 2008 2:23 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
cabernet1976
Senior Developer
Senior Developer


Joined: 16 Nov 2007
Posts: 154
Location: China

PostPosted: Thu Jan 17, 2008 1:15 pm    Post subject: Reply with quote

I make a big mistake Embarassed

The following is my lesson:
1. I created a sdcard, and put the sdcard.img in D:\dir1\sdcard.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. Rolling Eyes 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
Back to top
View user's profile Send private message Visit poster's website
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2660
Location: College Park, MD

PostPosted: Fri Jan 18, 2008 7:06 pm    Post subject: Reply with quote

Hello cabernet1976,

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

Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
tsdt
Junior Developer
Junior Developer


Joined: 09 Jan 2008
Posts: 12
Location: VIETNAM

PostPosted: Sat Jan 19, 2008 6:12 am    Post subject: Re: read file Reply with quote

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
Java:
FileInputStream fIn = openFileInput("/data/data/com.myname.android/files/JScript.htm")

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
Back to top
View user's profile Send private message
krystox
Freshman
Freshman


Joined: 22 Jan 2008
Posts: 7

PostPosted: Tue Jan 22, 2008 2:50 am    Post subject: Re: Working with Files Reply with quote

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?
Back to top
View user's profile Send private message
cabernet1976
Senior Developer
Senior Developer


Joined: 16 Nov 2007
Posts: 154
Location: China

PostPosted: Tue Jan 22, 2008 9:26 am    Post subject: Reply with quote

Hi krystox,

I met the same issus ( http://www.anddev.org/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-developers/browse_thread/thread/64fb55b5bd03a2cd , it is said that somebody has done it successfully.
Hope you can make it work.

_________________
Upload2Flickr's blog: http://upload2flickr.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page Previous  1, 2, 3, 4, 5  Next
Page 2 of 5

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.