cant find files written to device - android

I am using
outStream = ctext.openFileOutput("demo.jpg",0);
outStream.write(data);
outStream.close();
If I use the emulator the file is written to the data hieracrchy on the simulated sdcard.
When I run on my Samsung 15500 I cant find where the data has been written to. If I click on MyFiles I can see the directories under Android with a data subdir but no sign of the file.
Am I missing something either in the method of writing the file or the way to find it on the device.

You should be able to find it via Context.getFileList();. The docs to Context.openFileOutput() say
Open a private file associated with this Context's application package for writing
So I guess there is no "guarantee" that you can have arbitrary access to it via file paths or urls or such.

Related

Android: Storage Access Framework - Just need to copy a file to extSD

Existing Xamarin Forms app on Android.
New feature request from the bosses: To copy some updated documents from a USB FlashDrive to a known directory on the extSD already in the tablet.
Seems like that shouldn't require a PhD. Everything we've done to date has been on the internal storage so typical System.IO calls work fine. But Android has this whole Storage Access Framework stuff in place for things like the external SD, or Google Drive or where ever.
Every question and tutorial I see are all for the same thing: How to open a document browser to the SD card, or how to create a new CREATE DOCUMENT INTENT.
I'm not looking to do any of that. Don't need a UI. Not making a new text file from scratch.
I just want to copy a file from the flashdrive to the extSD. Everything else is in place to do this to internal storage. Easy-Peezy.
Its a single line when copying from the flashdrive to the internal SD.
FileInfo.CopyTo(targetAddress, true);
How do I copy a file to the extSD? Do I really need 200 lines of overhead making a DOCUMENTPROVIDER, and CONTRACT and 20 other things just to copy a file? All I get is "ACCESS TO extSD 1234-5678 is denied". Seems like it should be fairly simple to get permission to the card then just copy a file or make a new folder. But I swear everything I read for SAF makes it sound like you have to make 10 classes and a manager for them all first.
Anyone got a simple example of the minimalist way to get write permission to the extSD and copy from A to B?
If it is copying only one file them its pretty simple. Let the user select the fle with ACTION_OPEN_DOCUMENT. You get an uri for the file. Now let the user create a file at the right place using ACTION_CREATE_DOCUMENT. You got another uri. Now open an InputStream and an OutputStream for those uries. Then read bytes from input and write to output. Less then 200 lines ;-)

Android - How to download a file, then open it without saving them to disk?

I want to implement a "View" functionality in my app where the file is opened,but not actually saved to disk.
How do i download the file, then open it without saving to disk?
(I've noticed that a lot of other apps that do this don't need the storage permissions to open files so they surely must not be saving it to disk right?)
Or should i create a temporary file and then delete it when i'm done with it?
You don't need a permission for saving a file in the internal files directory. That's what most apps do. If you don't want to do that, you can read the file to a String object, and process it from there. You can use Apache IOUtils.copy for that.
Hereenter link description here is a stackoverflow question where it's described. You could have found that yourself by using Google.
If your concern to secure that file, you can simply store that file into your app's internal memory as private file, No app can open that file without your app context. To store files in internal memory:
File theDir = context.getDir("theDir", Context.MODE_PRIVATE);
File fileWithinThatDir = new File(theDir, "yourFile");
FileOutputStream out = new FileOutputStream(fileWithinThatDir);
to delete that file:
new File("PathOfThatFile").delete();
Download the file to a ByteArrayOutputStream or directly to a byte array. That is all in memory.

When using emulators, how to interpret the output path when calling Context.getFilesDir()?

Inside onCreate() I have this line:
File aux = context.getFilesDir();
which outputs this:
/data/user/0/com.example.tirengarfio.myapplication/files
but.. where is this path exactly insde Linux filesystem? or is it taking as reference the Android Studio Project root directory? os should I create it somewhere?
EDIT:
As I said to #Simas, but by the moment Im not connecting any smartphone. Im just using emulators on Linux. My intention is just reading a file using
FileOutputStream fos = openFileInput(FILENAME, Context.MODE_PRIVATE);.
So, where should I place the file inside the Linux filesystem?
This is the path of your app's local data folder. It can either be in the memory card or the device storage.
There's no easy way to access it if your device is not rooted but here's a starter:How to access data/data folder in Android device?
Let us see what the doc says :
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
No permissions are required to read or write to the returned path, since this path is internal storage.
So actually the files you will find are those which were saved with the very same function openFileOutput(String,int) by your/other applications.
So basically if you want to test some functionality (which I suppose) write a unit test that uses this API openFileOutput(String,int) to store some mockup data and then get it again with Context.getFilesDir() and some code for file processing.

Using a file located in either the raw or assets folder

Currently I am able to download a file off the internet and store on the SD card, then use the file from there. However that makes the file (with proprietary data) available to be seen. I would prefer to use the file from somewhere like raw or assets folder.
I will skip the downloading code, but my code to use the file is this
File myFile = new File (android.os.Environment.getExternalStorageDirectory() + "/folder/filename.xml");
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(myFile));
Android opens the file with the default application and all is good.
I have found similar Q/A's that revolve around using code like
Uri.parse("android.resource://com.projectname.testing/raw/filename");
and
InputStream ins = getResources().openRawResource(R.raw.filename);
but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code
I would like to solve my problem by simply accessing the file as a file. However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes. This sounds like a lot of extra work, especially since my code above does work for a file stored on the SD card.
Does anyone have any ideas?
Thanks
I would prefer to use the file from somewhere like raw or assets folder.
Note that these too can be "seen".
but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code
setData() does not take a File. It takes a Uri. Use Uri.parse() to parse other types of Uri values -- you already have this code shown above.
However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes.
It definitely will need to be world-readable. Also, not all apps support all schemes, so apps that support file:// or http:// might not support android.resource://.

Writing to the internal private storage in Android

I am trying to save some data from my app in a simple text file on the internal private storage area (app user preferences). I have read through many questions on here (StackOverflow) and tried the solutions suggested with no success. The simplest solution, it seems, would be the one suggested here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal but I cannot get this to work on my test device. I have also tried to create the file using the methods available in the java.io.File with the appropriate methods. I have also tried to create the file on the SDCard with the same result, fail. I have tried many solutions listed in other answers, following the code and instructions suggested exactly and find the same result. I am beginning to feel that I am missing some important bit of code, or a setting flag somewhere, I have set the permission in the manifest file:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
To be clear, I am trying to write to the device's internal, private storage. It is a small file containing a name, phone number, and a couple of type int flags. What ever method I use, I either find that the file did not create (or change if I place the file manually on the SDCard), or I get a NullPointerException when I try to reference the file or file location:
private File fILE = new File("Mydata", main.FILENAME);
or
private File fILE = getDir("Mydata", 0);
I am running the code on a HTC Hero, updated with the latest service release from Sprint. Any help would be GREATLY appreciated, Thanks in advance!
-Steve
Update (2/2/11): Using a EVO (API 8) I still get a NullPointerException. The code generating the exception is below, any thoughts on why my app can't access the internal storage? I have this problem on three different physical devices using two API levels (API 7 and 8).
File newfile = new File(this.getFilesDir() + "/data/files/", "sample.txt");
UPDATE 2: 2/4/11 - I have found that I cannot see the file structure on the physical device (data directory) under any circumstance. Any one have any thoughts on this? The device is properly configured and can run app from eclipse or adb.
UPDATE 3: (2/9/11) - I think I may have found what the problem is here, but I am not sure about how to deal with it. I have figured out that the permissions on the /data/ directory on the physical devices are: drwxrwx--x. I am not sure why it is this way, maybe something to with Sprint? I have found this set this way on an HTC Hero, Samsung Epic (Galaxy S), and HTC EVO all on Sprint. The issue appears to be that DDMS and my app do not have r/w access to the directory. I need to figure out 2 things here, why it is like this and how to over come this issue in the wild. Again, any help here would be AWESOME!!
UPDATE 4: I think last February was a total blonde moment for me (see UPDATE 3). The test devices that I have are not ROOTed and hence no access (DUH!). After all the updates that he SGS and the EVO 4G have gone through, the result is still the same. I am still working this problem and will try and get back here with an update soon (hopefully less than a year next time).
Try changing the following line:
File newfile = new File(this.getFilesDir() + "/data/files/", "sample.txt");
to:
File newfile = new File(this.getFilesDir() + "/","sample.txt");
Not a direct answer to your question, but nevertheless: I noticed that you don't want to store tons of data in your file. Would it be a sufficient alternative to use the Shared Preferences instead?
And perhaps even more interesting: does the problem occur even when you write to the Shared Preferences file instead?
http://developer.android.com/guide/topics/data/data-storage.html#pref
A physical device's /data/ directory is only available to the root user. Since the only realistic way to get this access on a physical device is to root the device, DDMS file explorer cannot get into this branch of the directory tree.
As to why the app will not write there, likely the issue is in the fact that I have signed the app with debug keys (not a big *NIX expert, but what appears to be the case here from my personal research).
I was dealing with the same issue. Finally, I found that you really don't have to give all file paths in order to create a new file in internal storage. Just mention the file name and it will be created under your app's package folder at the device. I did exactly mentioned here
And it works perfectly. I would say avoid mentioning Full file path like : /data/... in order to create a file (you need write permissions to create a file in such a manner). Let Android framework do the job for creating a private file for your app.
The internal storage area is sort of private to the application so the user must have root access(rooted device) to create and load the files. If you have rooted device this is how to write a file to the internal storage:
// Create a file in the Internal Storage
String fileName = "MyFile";
String content = "hello world";
FileOutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, Context.MODE_APPEND);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
This should instantly create a file called MyFile with the content hello world. The internal storage directory is usually found in a special location specified by our app’s package name. In my case it is /data/data/[package name] and the files created are stored in a directory called files in that directory.
As #bianca says, you're better not using a file path. But instead, use only the filename to create a File. Something like this:
public static void saveTextToFile(Context context, String filename, String content) {
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
And to get the file, you can use:
File file = new File(context.getFilesDir(), filename);
Read more: Saving Files.

Categories

Resources