How to write to local PC file from Android emulator app - java

I've been searching for hours and came across a lot of non-sense about read-only this and that and folders and directories and viewing the phone fs, etc. etc.
All I want to do is write to a file that is on my PC for an Android project. I've tried to use local paths (Windows) and have been met with read-only fs system problems, even after completely converting the desired folder to read AND write, Android refuses to get it. I set uses-permissions in my manifest with WRITE_EXTERNAL_STORAGE and WRITE_INTERNAL_STORAGE, but no luck.
I've tried to write to the phone, via emulator and actual physical phone, but every search I make turns up nothing (searching for the name of the file).
How can I write to a log file, any file that is where my development source code is? I need to digest JSON data from a web service call and I need it to be written to a local file so that I can work with it. The calls are through Android, currently an emulator, and an install on a physical device, yet no log is written because either a read-only problem which is non-sense, or I just can't find the damn file.
Any help would be greatly appreciated.

I guess ultimately the question I asked was just too messy. I wanted to know where I could find "local" logs on my Android device, which I can open and read myself in some text editor. I also wanted to know where "local" logs/files are written to my hard disk when testing the app via emulator.
At this point, I haven't found anything relating to a log file or any arbitrary file being able to be written from an Android app to a local dev PC without being pulled from something like adb.
I have found the solution to where I can write arbitrary files to an emulator or phone's filesystem (Source: https://www.journaldev.com/9383/android-internal-storage-example-tutorial):
FileOutputStream fos = Main.getAppContext().openFileOutput( "myfile.txt", Context.MODE_PRIVATE );
fos.write( "Hello, world".getBytes() );
fos.close();
Some things to note (Android SDK 26+ at least, others are untested):
FileOutputStream works very well.
"Main" refers to whichever entry point class you're using for your app. It most likely (or must?) extends AppCompatActivity.
Context.MODE_PRIVATE seems to be required.
AndroidManifest.xml permissions READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, READ_INTERNAL_STORAGE, WRITE_INTERNAL_STORAGE do NOT seem to be required.
Important: Files will be saved to /data/data/YOUR.APP.PACKAGE/files/.
This was also helpful: https://developer.android.com/training/data-storage

Related

Java, Codename One: Exporting and -most importantly- importing files between users

Here the problem: my app will generate some files, and I want to give to the users the opportunity to exchange these files between them.
This requires 3 steps:
Saving the data: easily done in Storage implementing the
functions required by the Externalizable class;
Sharing the data: done (probably, right now it's impossible to check if the
result is correct because the missing step 3) with the sharing
methods offered by the framework, as soon as I understood I needed
to use as mimetype "application/octect-stream";
importing the downloaded data (shared by another user): this one I can't manage to
find a way to make it work. Loading the files from the app's Storage
is easy, but accessing to the folders out of the app's Storage is
something I can't manage to do.
I used FileSystemStorage in the hope of gaining access at least to the Download folder that (mostly) every phone has, but apparently I can't manage to accomplish the task
Using the FileSystemStorage on Android, for example, I have access to
/storage/emulated/0
/storage/emulated/legacy
file:///system
The first two being related to the Storage of the app.
Acceding to file:/// I obtain a long list of folders, a partial list including
media
logs
sdcard
Removable
...
root
...
But when I try to access some of these, they all appear to be empty. Either I make some mistake or the app can't see their content.
So I wonder if there is a way to accomplish the task, namely to have access to the files (probably in the Download folder) the user has downloaded, to import them.
Phone apps live in isolation within the phone. Android literally creates a separate Linux user for every app so they don't step on each other and damage the phone. iOS does similar tricks.
As a result apps can't just write a file to downloads and can't just list all the files there. This would violate the users privacy. So the native API to share a file is usually separate from the files API. We do have a share API in the CN class which lets you share images etc. to an arbitrary OS app. See isNativeShareSupported and share.
Ok, maybe I found a solution for reading the files from the Download folder in an extension of CodenameOne called FileChooser.
According to this blog post it should give access to, between the others, the Download folder (at least in Android).
I'm going to try it and, when everything is ready and tested, edit this reply to say how it worked out for me.

Copy files from Android's Gdx.files.local() folder to developing machine

LibGDX: I have game-data files created at runtime for debugging purposes (a replay system). The files are stored in Gdx.files.local() whether on desktop or android. Now I want to copy the ones that were created on android so I can debug them on desktop.
Running Gdx.files.getLocalStoragePath() on android returns /data/user/0/com.[my domain].[my app]/files/. Using Windows Explorer I do not see my app's folder in /data/user/0, or anything else for that matter. So private storage indeed it is, but how nonetheless can I get hold of these files, without root if possible?
The solution may be manual copy or in code, whatever works. Just to note, the file names are unknown without seeing the files first. They were named using current date-time. The extension is known, though.
EDIT
I discovered Android Studio's Device File Explorer. I connected my device, tried to see inside my app folder and app data folder, both showing 'Operation not permitted'.
So to put the question in simpler words, is rooting the phone really the only way for developers to see the files created by their apps?
Or is there a magical gate somewhere known only to select few zen masters who guard the secret through the generations? Because I sure ain't finding it.
I'm not sure that there is a way to write changes in the system file without having root access but you can read them. According to this Blog, You can access those files and put it in your desktop. You can download RAR app from googleplay.

Is my MainActivity File corrupt after PC failure?

I was using Android Studio, latest release yesterday when my PC decided to just turn off(Turns out PSU's dont last forever). I took the HDD out until I can find a replacement PSU and put it into another PC, upon opening my project I can open the MainAcivity anymore with Android Studio. I opened the Java file with a text editor and it comes up with about 8000 lines of 0's.
Does anyone know how to fix this as I put alot of work and time into that file?
Your file is corrupted, the sector of the disk where the file is stored is effectively damaged / unallocated because the file was being held in the disk cache / currently written.
Resetting the file from version control
If you use a version control mechanism such as git, mercurial or cvs, you can try check-out the file from the system from the moment of your latest successful check-in. If that fails you can try cloning/checkout out the online version of your repository and see if that is the correct version to.
Recovering the file from backups
Every person SHOULD have a proper backup system, making backups is relatively easy to do, and even a git repository stored on multiple computers can already be a good backup.
Even Jeff Atwood has made this mistake in the past. (yes, he is user 1)
Recovering the file using disk checking applications
Sometimes, you may have more luck using chkdsk on Windows or fsck on Linux.
This may happen because you worked on the file, and the file system decided to move the file to another sector for various reasons. Then, while it was moving the file, the hard disk crashed. This left the file pointer referring to the new location, while the file is still safe at the old location.
On Linux and Mac, files recovered by this technique are stored in directory /lost+found, while on Windows, this directory is called "/Found" located in the affected hard drive.
Decompiling your old application
Sometimes, the above techniques are delivering anything resembling your original code. In this case we need to use our must ugly method, decompiling.
There are various java and android decompilers you can use, these can be easily found on google by searching for "java decompiler". I am not going to name them here to prevent this answer from becoming opinium based on what decompiler is the best.

Protect and unprotect file to avoid accidental elimination in Android by bugged cleaning apps

In my Android App I save some files with some data file using
FileOutputStream savedList = new FileOutputStream(Path);
in a folder named myApp located in the SD storage
Unfortunately I have noticed that some cleaner Apps, not well implemented, also very popular (e.g. CleanMaster) wrongly remove the files every time the user perform a temp\trash file cleaning causing problems.
Is there a way to protect (and unprotect for writing) the file programmatically to avoid this?
How to change file permissions?
Since aren't used the file extensions to recognize the file format, how could I change the metadata of the file that are used to determine the file format so that these file are see as documents by these apps? I suppose that the scan of these Cleaners use some strategy based on Linux file format recognition and remove all object files.
Android allows to have private directory on SD card for the app.
you can get the path for private directory for your app as follows.
File myDir = getExternalFilesDir(null);
The null parameter indicates that you are going to store any type of files in the directory
myDir.mkdirs();
Log.d("info", myDir.getPath());
These files are internal to the applications, and not typically visible to the user as media.
This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:
Shared storage may not always be available, since removable media can be ejected by the user. Media state can be checked using getExternalStorageState(File).
There is no security enforced with these files. For example, any application holding WRITE_EXTERNAL_STORAGE can write to these files.
This solution worked for me as cleaning apps on devices don’t clean these folders considering them as private folders for the respective apps.
Checkout following link from android docs. Context.getExternalFilesDir(java.lang.String)
Write it to your private internal drive, so they don't have permission to touch it. SD cards are just FAT32 drives, so they don't support file permissions or access lists.
On 4.4 phones you may be ok, as Google basically prevents any writes to the SD card outside of a private directory. Cleaner type apps won't work on it at all, for better or worse.
First, you should read the first answer of this question. The thing to remember :
No, Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage". On some devices, this is removable media, like an SD card. On some devices, this is a portion of on-device flash.
The SD card is a vague notion, it's quite impossible to be 100% sure you are writting on the SD card.
That thing said, you should use the Android API to write your file on the private directory of the app, located in /path/to/external/storage/whatever/it/is/on/the/device/Android/data/com.package.yourapp/files
Use getExternalFilesDir to get the above File and write your file on the private directory of your app, this way, no one will be able to delete it.

Android: Downloading and Handling Unknown File Type

I'm developing an Android app that communicates with a microcontroller via Bluetooth. I am able to pair and connect with it but I want to send a file that I've downloaded through the Android browser through Bluetooth.
Although, the file type that I hosted online and downloaded is a custom file type that Android does not recognize.
I've read and tried the solutions in this thread: Register new file type in Android
but had no luck. I tried adding the intent-filter but I had a feeling it had to do with the Android file system and not my app itself.
Is there anything that I'm doing wrong? What would be the best way attack this?
tl;dr: How do I get Android phones to recognize my custom file type so that it can be used? (My phone does not allow it to be downloaded since it does not recognize the file type).
If you can't get Android to recognize your file, you could try changing the file extension to a known type ( ie. foo.yft to foo.jpg or perhaps even no extension? ) and then either letting your micro use it with the fake extension, or adding a bit of logic to rename it after the transfer.

Categories

Resources