How to share my files to other packages?

Put your problem here if it does not fit any of the other categories.

How to share my files to other packages?

Postby devinyu » Fri Dec 05, 2008 9:00 am

Hi all,

I need your help.

If there are 2 packages, and package A created a file and wanted to share it to package B.
How could I start? Does setting a Content Provider help me to overcome the problem?
If Content Provider is a good choice, please tell me the tips; Otherwise, how can I do?

Could someone give me a favor?
Thanks a lot.
devinyu
Freshman
Freshman
 
Posts: 5
Joined: Fri Dec 05, 2008 8:43 am

Top

Re: How to share my files to other packages?

Postby Jay » Fri Dec 05, 2008 10:15 am

devinyu wrote:Hi all,

I need your help.

If there are 2 packages, and package A created a file and wanted to share it to package B.
How could I start? Does setting a Content Provider help me to overcome the problem?
If Content Provider is a good choice, please tell me the tips; Otherwise, how can I do?

Could someone give me a favor?
Thanks a lot.


Hi devinyu,

Try to set the flag WORLD_WRITEABLE AND WORLD_READABLE when creating your file .
Or simlply set the sharedUserId of the two packages to let them have a same file access authority.

Regards,
Jay
Jay
Developer
Developer
 
Posts: 28
Joined: Wed Apr 23, 2008 3:51 am

Postby devinyu » Fri Dec 05, 2008 10:46 am

As you said, how could I read file from other package in code?

for example, I wrote code in package A as below:

FileOutputStream bakfile = openFileOutput("123txt", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(bakfile.getFD()));


and try to read in package B through

FileInputStream orifile = openFileInput("/data/data/[b]com.example.android.filehandle[/b]/123.txt");

Can I read files directly in package A (as colored)? is it right?
devinyu
Freshman
Freshman
 
Posts: 5
Joined: Fri Dec 05, 2008 8:43 am

Please help me~~~

Postby devinyu » Mon Dec 08, 2008 4:11 am

Could someone help me?

I created a file in, for example, /data/data/com.example.android.filehandle/test1.txt, and set its attribute with
MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE.

And now, if I want to access test1.txt in other package, com.example.andorid.testfile,
how do I access test1.txt?

I tried the way as below:
FileInputStream orifile = openFileInput("/data/data/com.example.android.filehandle/test1.txt");
Unfortunately, it fails.

And, I also trid to set "com.example.android.filehandle" as SharedUserID in both package.
But it still can't find file (test1.txt).


Please help me and point my wrong, thanks a lot.


sample code:

package "com.example.android.filehandle"

FileOutputStream fOutput = openFileOutput("test1.txt", 3);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fOutput);

int length = 0;

length = fInput.available();
PrintStr += "Original File Size: " + length + "n";
ptxtRead.setText(PrintStr);

int ch;
PrintStr += "Backup content :n";
while((ch = inputStreamReader.read()) != -1)
{
PrintStr += (char)ch;
outputStreamWriter.write(ch);
}
}

.....


in package "com.example.andorid.testfile"

FileInputStream orifile = openFileInput(("/data/data/com.example.android.filehandle/test1.txt");
BufferedReader bufReader = new BufferedReader(new FileReader(orifile.getFD()));


String SaveStr = null;
while((SaveStr = bufReader.readLine()) != null)
{
// bufWriter.write(SaveStr);
//bufWriter.newLine();
}
devinyu
Freshman
Freshman
 
Posts: 5
Joined: Fri Dec 05, 2008 8:43 am

Re: Please help me~~~

Postby Jay » Mon Dec 08, 2008 4:46 am

devinyu wrote:Could someone help me?

I created a file in, for example, /data/data/com.example.android.filehandle/test1.txt, and set its attribute with
MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE.

And now, if I want to access test1.txt in other package, com.example.andorid.testfile,
how do I access test1.txt?

I tried the way as below:
FileInputStream orifile = openFileInput("/data/data/com.example.android.filehandle/test1.txt");
Unfortunately, it fails.

And, I also trid to set "com.example.android.filehandle" as SharedUserID in both package.
But it still can't find file (test1.txt).


Please help me and point my wrong, thanks a lot.


sample code:

package "com.example.android.filehandle"

FileOutputStream fOutput = openFileOutput("test1.txt", 3);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fOutput);

int length = 0;

length = fInput.available();
PrintStr += "Original File Size: " + length + "n";
ptxtRead.setText(PrintStr);

int ch;
PrintStr += "Backup content :n";
while((ch = inputStreamReader.read()) != -1)
{
PrintStr += (char)ch;
outputStreamWriter.write(ch);
}
}

.....


in package "com.example.andorid.testfile"

FileInputStream orifile = openFileInput(("/data/data/com.example.android.filehandle/test1.txt");
BufferedReader bufReader = new BufferedReader(new FileReader(orifile.getFD()));


String SaveStr = null;
while((SaveStr = bufReader.readLine()) != null)
{
// bufWriter.write(SaveStr);
//bufWriter.newLine();
}



Hi devinyu,

Open the DDMS file explorer , and check the file that you've just created .

Dose it lay in the /data/data/[your pkg] /files/[your file] ?

If it dose in there , i think you know what to do .

Regards,
Jay
Jay
Developer
Developer
 
Posts: 28
Joined: Wed Apr 23, 2008 3:51 am

Postby devinyu » Mon Dec 08, 2008 5:03 am

I'm sorry, I think I did a typing error.

But I still have problem on that.

As below, I created my file in data ...

test.txt 10 2008-12-05 08:17 -rw-rw-rw-

And, when I go step to this instruction and it fails (not a fileNotFound exception, just jump to activitythread ....,
I don't know what it is)
Code: Select all
        try
        {
           [color=red]FileInputStream orifile = openFileInput("/data/data/com.example.android.filehandle/files/test.txt");[/color]
           BufferedReader bufReader = new BufferedReader(new FileReader(orifile.getFD()));
}


Could you explain more to let me know?
devinyu
Freshman
Freshman
 
Posts: 5
Joined: Fri Dec 05, 2008 8:43 am

Top

Postby Jay » Mon Dec 08, 2008 6:55 am

devinyu wrote:I'm sorry, I think I did a typing error.

But I still have problem on that.

As below, I created my file in data ...

test.txt 10 2008-12-05 08:17 -rw-rw-rw-

And, when I go step to this instruction and it fails (not a fileNotFound exception, just jump to activitythread ....,
I don't know what it is)
Code: Select all
        try
        {
           [color=red]FileInputStream orifile = openFileInput("/data/data/com.example.android.filehandle/files/test.txt");[/color]
           BufferedReader bufReader = new BufferedReader(new FileReader(orifile.getFD()));
}


Could you explain more to let me know?


Hi devinyu,

This is the document of the method openFileInput:

public FileInputStream openFileInput(String name)
Open a private file associated with this Context's application package for reading.

I think it means you can only access local files of this application by using this method.

can you try the java way to open the file ? And let me know if or not it works.

Regards,
Jay
Jay
Developer
Developer
 
Posts: 28
Joined: Wed Apr 23, 2008 3:51 am

Postby devinyu » Mon Dec 08, 2008 7:00 am

Thanks a lot, I just tried another to access before lunch.
And it works as my expectation.

I changed code as below:

Code: Select all
           FileOutputStream bakfile = openFileOutput("12345.bak", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
           BufferedWriter bufWriter = new BufferedWriter(new FileWriter(bakfile.getFD()));
           
           BufferedReader bufReader = new BufferedReader(new FileReader(new File("/data/data/com.example.android.filehandle/files/test.txt")));


I appreciate your great help.
Thanks again.
devinyu
Freshman
Freshman
 
Posts: 5
Joined: Fri Dec 05, 2008 8:43 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests