Access an app's "private storage" folder (Internal Storage) - android

MinSDK=24, Target/Compile=27, Java 8
I'm creating a file inside my app's InternalStorage folder with this code:
File f = new File(context.getFilesDir(),"settings.txt");
if(!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
According to getContext().getFilesDir().getPath(); the folder is data/user/0/com.xxx.xxx/files (I'm aware this can change through updates,...) and if I run above code on Android Studio's simulator, the new "settings.txt" file can be accessed through Android Studio's "Device File Explorer".
This doesn't work on an actual phone though (Galaxy S7 with 7.0) because the user folder doesn't exist according to the "Device File Explorer".
I know that an app's InternalStorage can't be accessed by other apps, so Android's default File Manager probably won't display it but is there a way to access the folder via e.g. Win 10's explorer on an actual Android phone without rooting it? If so, how?

Related

Unity: Accessing .txt files located on Android device connected to PC

I was wondering if anyone could possibly help me or give some advice on how to access a text file that has been saved onto an Android phone which is connected to a PC using Unity? Basically I am trying to track a player's heart rate using an Android Wear watch (Huawei Watch 2) and then send that data over to my phone (Huawei P10) which is connected to my PC, where it can hopefully be read by Unity. So far I have been able to collect the heart rate data and send it over to my phone where it is saved into a text file in the external storage (in this case storage/emulated/0). My next step is to read the text file in Unity but I am unsure which is the correct way to do this and searching online has not provided any solid leads and I was hoping someone may be able to point me in the right direction. Apologies if this question is a bit vague and thank you in advance.
First i would get the path of that text file in my logcat
Log.e("PathOfFile",""+filePath);
after i get the path of the file i would go to device monitor in Android Studio, and with my phone connected to pc i will go to storage/emulated/0
Important Note: storage/emulated/0 is located into internal memory, so you are not able to see this text from your phone (if no root was made) , but you can see this file in debug mode from Android Studio
after that you can navigate to your text file
The reason that the file is stored at internal memory might be because you are creating it into internal memory like this
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(fileContents.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
to save it in external storage you should do this
public File getPublicAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
check the official doc for more info here: https://developer.android.com/training/data-storage/files.html#java
happy coding

MediaScannerConnection#scanFile converts directories into files when accessing them with USB-MTP on Android 5 Lollipop

Changes like renaming a file triggered by an app only appear to the USB-MTP interface after reboot of the Android device or after you registered the new file at the MediaScanner them like this (see Trigger mediascanner on specific path (folder), how to?):
file.renameTo(newFile);
MediaScannerConnection.scanFile(context,
new String[] { newFile.getAbsolutePath() }, null, null);
USB-MTP is used to access the storage of an android device via USB. E.g. with the Windows Explorer.
However, with the Sony XPERIA Tablet Z (SGP321) under Android 5.0.2 (Build 10.6.A.0.454) folders supplied in newFile will become a file with 4KB. I am no more able to access the folder structure using Windows Explorer anymore, nor can I copy the file to my computer. Even after reboot of the tablet! The same device with Android 4.4.4 does not show the behavior. It appears that only the USB-MTP view is broken. The file structure accessed by an android app still looks fine.
Question: Is this behavior a bug or did I implement it incorrectly? What would be the correct implementation?
What I've tried so far to fix the issue:
My current workaround is to avoid scanFile for directories.
I can convert files back into directories by renaming them with an android app without MediaScannerConnection#scanFile. After reboot, I can access the directory with Windows Explorer again.
Renaming files with Windows Explorer that actually are directories does not restore them. Even after Reboot.
This line as suggested in https://stackoverflow.com/a/21918085/433718
does not refresh USB-MTP view, but also does not convert directories
into files:
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(newFile.getParentFile()));
Maybe related:
https://stackoverflow.com/a/27321544/433718
Using content resolver for all sorts of File operations like deleting a file in this answer: Android Deleting Files MediaScannerConnection
I ended up creating a dummy text file in each directory I wanted to make visible, and use scanFile on the file.
1) create directory, but don't "scan" directories
2) copy file to directory
3) run scanFile on the filePath
MediaScannerConnection.scanFile (_application, new String[] { filePath }, null, null);

Android: save file to downloads that can be viewed later

I have an app that needs to collect a bunch of data while connected to a stream. I need to save this data to a file that I can later pull off my device and analyze using a standard computer.
My code currently looks like:
private void saveData(byte[] data){
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "_Ascent_Test.txt");
try {
FileOutputStream stream = new FileOutputStream(file, true);
stream.write(data);
stream.close();
Log.i("saveData", "Data Saved");
} catch (IOException e) {
Log.e("SAVE DATA", "Could not write file " + e.getMessage());
}
}
It's correctly hitting the "Data Saved" log without any errors yet I cannot find the file anywhere in the devices internal storage when I browse it from my computer.
What am I missing?
Thanks.
Edit: I'm needed to run this on a Nexus 7
Files are not visible unless you make them explicitly available. See my blog post about the MediaScanner to read more about this.
It's the developer's job to take care of this and to make sure that all files, the user might want to access, are made available to the MediaScanner.
I wanted to do a very similar thing. Here's how I got it to work using your code EXACTLY.
After running my code to make the file(/storage/emulated/0/Download/_Ascent_Test.txt):
I downloaded the ES File Explorer.
In the "Fast Access" menu towards the bottom I turned "Show hidden files" ON.
Then, also in the "Fast Access" menu, go to local -> / Device.
Now you will be able to navigate to the /storage folder and all the way down to _Ascent_test.txt
From there you can open it and email it yourself.
Hope this helped!
So this seems to be a known issue with Nexus 4 and 7. I still don't have a workaround, but for now, using Astro File Manager to email myself will solve the immediate issue at hand.
Saving files on external storage on Nexus 7 and retrieving from PC
Nexus 4 not showing files via MTP
Always fun to waste several hours on something like this. Gah!

Created folder shows on device but not on PC

I am trying to create a folder that users can put stuff in and use in the app so I do this
File exportDir = new File(Environment.getExternalStorageDirectory()+"/BCAData");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
file.createNewFile();
If I use a file explorer program like Astro File Manager I see the folder fine but when I plug the device into my PC the folder is not there to put stuff in.
is there something else that I have to do for it to be used on a PC?
At minimum, you need to get MediaStore to update its index, such as via scanFile() on MediaScannerConnection, as I outline in this blog post.
Depending on your PC's operating system (and possibly depending on the device), you may still not see changes while the device is plugged in. It appears that the PC does not necessarily "see" changes, even with the index updated, until you unplug and plug the device back in.

Android - problem with file-writing

I have some problems when writing an xml file onto my android device (GalaxyTab 10.1V). The code for writing the xml works fine. I can see the written file at the DDMS perspective in Eclipse and can save the file to any location on my computer. But when I open the same directory as opened in Eclipse with the windows explorer, it seems that the file does not exist! The file is still visible at the DDMS perspective, but windows explorer does not want (or canĀ“t?) show me this file. After rebooting the tablet, the file is also shown at the windows explorer.
I have tried several things for solving this problem...
writing a .txt file --> same result
tried getExternalFilesDir() (as recommended since API Level 8) and getExternalStoragePublicDirectory() instead of getExternalStorageDirectory() --> same result
writing at USB-Debug mode, writing at "normal" USB mode, writing without USB connection --> same result
of course I have set the permission to read and write on the device at the manifest file
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE
"show hidden files" is activated at the windows folder options
this is how my code looks like...
public boolean write(Result result) {
Serializer serial = new Persister();
File file = new File(Environment.getExternalStorageDirectory()
+ "/Test/result.xml");
try {
serial.write(result, file);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
I use the SimpleXML library for reading and writing my xml files but as mentioned before, the same problem occurs when writing a normal .txt file
Does anyone have an idea what the reason for this behaviour could be?

Categories

Resources