Android: writing simple file to external storage on Emulator - android

I am learning Android. I have the latest, as of today, Android Studio and SDK.
I have gone through the tutorial on "Saving Data" at developer.android.com. I have perused StackOverflow for a couple hours and can't seem to find precisely what I am looking for; so here goes.
My app will have a need to read and write from a directory on the Android device. This directory needs to be accessible when connected to my PC via Windows Explorer. I must admit I am confused by getExternalFilesDir(), getExternalStoragePublicDirectory() and all the other getFilesDir() methods.
Question 1: How do I create, access, read/write a simple text file to such a directory. It would be fine if this directory goes away if the user uninstalls the App. But the directory must be accessible to my App on the phone, and to Windows Explorer when connected to my PC via USB cable. Those are the only requirements.
Question 2: In trying to figure this out, I am using an AVD virtual device. When querying getExternalStorageState(), it says "Removed". Can I access such a directory when debugging using the AVD virtual device; and if so, what am I missing? I have the "WRITE_EXTERNAL_STORAGE" permission in my manifest file. I have also set my AVD with the "SD Card" set to "Studio-managed" and "200 MB".
Thanks.

Oh my. It turns out I was doing this correctly, but had updated my code and settings without restarting the AVD.
Once I restarted my AVD, the external storage (SD Card) showed as Mounted and I was able to read/write to the "Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);" directory.
I believe that this directory will be accessible from Windows Explorer when the device is connected to my PC.

Related

How to read/write external USB storage on Android?

I am currently making an app that needs to be able to read from and write to a USB flash drive connected via a USB OTG adapter. Is there an easy way to access this storage via standard Java.io.File APIs? This app is only going to be run on a rooted Motorola Xoom running Android 4.2.2. Any suggestions would be appreciated.
USB Drives get mounted to your device just like an SDCard does essentially*.
The mount path usually resides at:
/storage/usb0/
I have not used this on many devices other then my Droid running CyanogenMod, your device may very. You can smiply use a file manager to explore this path. The directories will still exist even if there is no mount path, so you will be able to determine the path.

Mounting Kindle Fire on a Mac

I'm working with someone who has a 1st generation Kindle Fire to test Amazon's in-app purchasing. According to Amazon's docs, we need to create a response file named /mnt/sdcard/amazon.sdktester.json on the device. My colleague prepared the .json file and tried to transfer it to the device via a USB connection. However, when he connects his device to his Mac, the root folder of the mounted device does not have a /mnt folder. He also tried mounting it on a virtual Windows machine on his Mac, with the same result.
I'm assuming that for some reason the Kindle Fire is mounted in a way that does not provide access to the root folder of the device. Can anyone recommend a way around this, or some other way to put a file into /mnt/sdcard/ on my colleague's Kindle Fire?
If there's a way to directly type in the file on his Kindle Fire, that would be acceptable; it's just a few lines of text. All suggestions are welcome.
When you say "transfer via USB connection", do you mean that you mount the device like an external drive, or are you accessing it via ADB? You will need to access the device via adb to get to the root folders.
So we figured it out. The problem, as Nick Davis pointed out, is that the root folder is only visible when connecting through adb. My colleague does not have a development environment and does not want to install it.
Instead, he installed the Text Edit app on his device, mailed the text to himself, copied it into a new document and saved the file directly to /sdcard/android.sdktester.json (which is an alias for /mnt/sdcard/android.sdktester.json).
This was practical only because there wasn't a lot of text involved. For large files, I suppose he could have copied them to a different folder through the USB connection and used a file manager like #ssantos suggested in a comment to move it to the right folder.

Accessing folders on my android phone that are visible to the user vi USB

When I connect my Nexus 4 to my PC via USB I get a nice set of folders such as Alarms, Downloads, and DCIM. I would really like to access these folders. However, when I use
Environment.getExternalStorageDirectory()
to do so I get /storage/emulated/0 as the returned directory path. When I create a directory or file there I am unable to see them when I browse using my PC. My output popup claims that the folder/file exists but I'm not able to see them. If I do the same thing in my emulator it always defaults to the "SD card" I mounted and I can see the folders fine.
How would I go about accessing and writing to these folders without root access. I've been searching all day, and even found a few new leads while typing this, but no luck.
Thanks

Make device SD Card windows bootable by an android app

It may seem strange, but I wonder is an android app able to make the SD Card inside the device windows bootable? I thought of making the SD bootable and write ultimatebootcd to it. I did it through windows, but it would be so great if android can do it!
To make the SD card bootable, you have to modify the boot sector of the card. This can not be done from a standard Android application. If the Android device is rooted, it can almost certainly be done from a native Linux program, or an NDK program.
If the device is not rooted, you can probably not do what you want. As Chris Stratton commented, if the card is already made bootable, you could probably then change for instance what OS is booted up and how.
Say, there is startup code which loads and boots an OS, then you can change the configuration file for the bootloader. A starter: http://www.syslinux.org/wiki/index.php/Boot_an_Iso_image
I used Rufus and installed a syslynux on the SdCard from a PC running window 10.then I put the card back into the Moto phone. It's receiving files that I have moved from the internal storage to the card how ever I have not a chance to find out whether they Will open up In the PC or not.

Where can I create directories that are visible to Windows Explorer and my application in Android?

I am developing an application that has the following requirement:
When the device is connected to a computer via USB, the user should be
able to use Windows Explorer to drag files into and out of the
application directory.
The application will consume some of these files and produces others
which should be able to be copied back to the computer.
The target device does not have an external storage device (for
example an SD Card).
The directories that the files will be stored in should be accessible
whilst the application is running.
I've looked into the data storage page in the android documentation, but none of the methods stated there seem to do what I require.
Alternatively, is there a way to select the computer when the application is running and 'send' files via USB to the computer? How about for 'receiving' files?
What is the best way to fulfill this requirement?
When the device is connected to a computer via USB, the user should be able to use Windows Explorer to drag files into and out of the application directory.
If "the application directory" refers to a spot on external storage, this is possible.
The application will consume some of these files and produces others which should be able to be copied back to the computer.
If this is supposed to happen simultaneously, the device must be running Android 3.x or higher. Android 1.x and 2.x did not support simultaneous access to external storage.
The target device does not have an external storage device (for example an SD Card).
It needs to have something designated as "external storage". That does not have to be removable storage. On Android 3.x and 4.x, it is usually just a part of on-board flash that was designated to be accessible as external storage. In Android terms, "external storage" means "can be accessed by a host computer using USB and by all applications on the device as well".
The directories that the files will be stored in should be accessible whilst the application is running.
Again, this requires Android 3.x or higher. If this is a custom device, that most likely means you are going to need to use the recently-released Android 4.0 source code.
Alternatively, is there a way to select the computer when the application is running and 'send' files via USB to the computer? How about for 'receiving' files?
No and no, respectively.
What your going to want to do is use Environment.getExternalStorageDirectory. Even if there is no sd card, most android devices partition a part of the on board memory to be used as external storage.

Categories

Resources