File exchange PC <--> Android via USB MTP issues - android

In my android app, I create a file and write some test text into it:
File externalPath = getExternalFilesDir(null);
File importPath = new File(externalPath, "pd-import");
if(!importPath.exists()) {
Log.d(this.getClass().getSimpleName(), "Create import dir: " + importPath.getAbsolutePath());
importPath.mkdirs();
}
File readme = new File(importPath, "README.txt");
try {
FileWriter fw = new FileWriter(readme);
fw.write("This is a test");
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
This writes the file, which can be approved with an android file browser like B1 File Manager. It's written to HOME/Android/data/JAVA_PACKAGE_NAME/files/pd-import/README.txt
The logcat shows me:
D/MainActivity﹕ Create import dir: /storage/emulated/0/Android/data/JAVA_PACKAGE_NAME/files/pd-import
When I connect my Nexus, where I tested the code, to my Ubuntu Laptop via USB, I see all the other applications data directories like NEXUS 5/Interner Speicher/Android/data/ALL_THE_OTHER_JAVA_PACKAGE_NAMEs ("Interner Speicher" stands for: internal memory). But the folder is not listed for my app is not listed.
Do I need to set some additional medatdata/information/whatever, to have the folder listed over the MTP connection? Anny suggestions?

Does the directory appear when the device is rebooted ? If yes, I think this bug is the cause : https://code.google.com/p/android/issues/detail?id=38282
All phones using MTP instead of USB Mass storage do not properly show
the list of files when that phone is connected to a computer using a
USB cable. Android apps running on the device also cannot see these
files.
This affects files written using Java APIs, but does not appear to
affect files written using the C API.
The solution is to reboot the device.
It does not seem to be resolved.

Related

Python automated script file transferring

I am looking for a solution for copying all the files from a specific directory on the hard drive, to a specific or non specific directory on my android phone, once this device is connected.
I would like these files to be automatically moved (or at least copied) to my phone once I connect it to the computer and run the .py file.
I have windows 7 and python 2.7
I was trying this from another answer but I can't understand because there is few explanation, therefore I cannot get it to work.
edit: I have figured out how to transfer files between to folders but I want to my phone. So how can I fix the error of my system not finding the path of my phone, that'll fix my problem I believe. The code works fine the problem is the path.
Here is my code:
import os
import shutil
sourcePath = r'C:\Users\...\What_to_copy_to_phone'
destPath = r'Computer\XT1032\Internal storage'
for root, dirs, files in os.walk(sourcePath):
#figure out where we're going
dest = destPath + root.replace(sourcePath, '')
#if we're in a directory that doesn't exist in the destination folder
#then create a new folder
if not os.path.isdir(dest):
os.mkdir(dest)
print 'Directory created at: ' + dest
#loop through all files in the directory
for f in files:
#compute current (old) & new file locations
oldLoc = root + '\\' + f
newLoc = dest + '\\' + f
if not os.path.isfile(newLoc):
try:
shutil.copy2(oldLoc, newLoc)
print 'File ' + f + ' copied.'
except IOError:
print 'file "' + f + '" already exists'
I am sorry I am being handful but I thought I had solved it.
In theory, there is no way to access your phone's internal memmory with a drive letter, because, Android connects as an MTP device, and not as a Mass Storage device. But, there are some weird solutions:
Root the phone and get a application which enables "Mass Storage" .
If you can not root and if(only if) both the computer are on the same network, run FTP server in you phone, and you get access for file copy by ftp.
But for you case I recommend adb- adb push C:\src /phone_destination is the best solution.You can google and easily find out way to do this in python.
Google offers adb-sync, which is also available in python. This allows backup/synchronization of files on android device to PC.
The following github repo provides instructions on how to setup the process ie: enable USB Debugging, etc... however I suggest installing 15 second adb installer as opposed to downloading/installing the massive Android SDK just to get adb.
adb-sync: https://github.com/google/adb-sync
15 Sec ADB installer: https://forum.xda-developers.com/showthread.php?t=2588979
A little late to answer, but I use SSH certs and crontab to run a ping command against my local IP and pipe that to an scp recursive copy. It will copy any changes over. No issue yet, and it's been running 4 years straight. I can't for the life of me find the command line that's running.

Can't locate application data on Android phone

I have spent many hours trying to find/create files for an app I am writing. When I pull the application directory name I get: /data/data/com.example.android.[myapp]/files. I am using File(getFilesDir():
File fileDir = new File(getFilesDir() + File.separator);
Log.i(TAG, "File directory: "+fileDir);
When I try to find this path I find many application folders here: Android/data/com but no /data/data folder under Android. There are many other application folders there but not mine. I see the same results whether I use Android Files app or Windows Explorer over USB. I've also tried to look using Eclipse DDMS tab. I see a data folder with a (+) to the left but when I click, it does not expand.
I have also tried creating the directory and file manually with Windows explorer and my app still can't find neither the Android/data/com.example... nor the Android/data/data/com.example... paths.
Also puzzling to me is when the app creates the path and file and write to it (using MODE_WORLD_WRITEABLE) I get no exceptions thrown but then I am unable to read it back or see it with either of the tools mentioned above. I have set the manifest permissions to WRITE_INTERNAL_STORAGE and WRITE_INTERNAL_STORAGE for the app.
Obviously, I am making a very basic mistake.
I am on Android 4.1.2 (API 16).
Sincerely,
ScratchingMyHead
To get the path of my application directory, Try this code sample
PackageManager m = getPackageManager();
String s = getPackageName();
try {
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
} catch (NameNotFoundException e) {
Log.w("yourtag", "Error Package name not found ", e);
}
When I try to find this path I find many application folders here: Android/data/com but no /data/data folder under Android.
That is because you are looking on external storage, not internal storage where your files are. Use DDMS on an emulator to examine internal storage.
I've also tried to look using Eclipse DDMS tab. I see a data folder with a (+) to the left but when I click, it does not expand.
That would sound like what you will get when testing on hardware, as neither you nor DDMS have access to the contents of /data on production hardware.

View app data file in Android File Transfer

I cannot find the file I created in my android app in Android File Transfer under /Android/data. My goal is to copy/edit a config file to the app data folder while I develop the app, and then later link it to a Google Drive file. But, even when I write a file in the app with the following:
FileOutPutStream outputStream;
try {
outputStream = mAppContext.openFileOutput("my_config.json", Context.MODE_WORLD_WRITABLE);
outputStream.write("test".getBytes());
outputStream.close();
Log.d("WRITE", "FILE WRITTEN!");
catch (Exception e) {
e.printStackTrace();
}
I get the debug log message the the file was written, but I cannot find it in Android File Transfer (there isn't even a directory for my app). I have also tried cd-ing into it from adb shell, as well as rebooting the device. What is the best way to quickly transfer a file into and out of an app directory? I am running 4.4.2 Kit Kat.

vold.fstab file in Android 4.3

I want to get the external and internal storage in my app. I'm using this code
Scanner scanner = new Scanner(new File("/etc/vold.fstab"));
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.toLowerCase().contains("dev_mount")) {
if (line.toLowerCase().contains("nonremovable")) {
VoldMounts.put(line.split(" ")[2],true);
}else {
VoldMounts.put(line.split(" ")[2],false);
}
}
}
It's working fine on Android 4.2.2 and below, but on Android 4.3 the file is changed to /fstab.<device> and this file needs root access. How can I read the fstab without the need of root access? I don't want to use /proc/mount.
Thanks.
Here is some official information that confirms that "for Android releases 4.3 and later, the various fstab files used by init, vold and recovery were unified in the /fstab. file". As that file has -rw-r----- root root permissions there is no way to read it from an app on a non-rooted device.

Accessing FAT32 and NTFS format USB Pendrive from android tablet

We have a USB port in our android tablet(version 4.0.3).
Pendrive File Systems Format are
NTFS
FAT32
When Pendrive File Systems Format are FAT32 File has been created Successfully. But When File Systems Format are NTFS, I got the Error Message as open failed: EACCESS (Permission denied).
I Need to create a New File from in the USB Pendrive. I have tried my sample code is
Button createFile = (Button) findViewById(R.id.createFile);
createFile.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try
{
File root = new File("/mnt/usbhost1");
Runtime.getRuntime().exec("chmod 777 " + root.getAbsolutePath());
File myFile = new File(root,"createNew.txt");
myFile.createNewFile();
Toast.makeText(getBaseContext(), "Done Creating File", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
Here /usbhost1 is a Android tablet USB Path. Where I am mistaken. How to create a New File from in the NTFS File Systems Format.
Thanks in advance.
Regards
Bala
What you need is a way to enable support for NTFS in the kernel on your device. This could be achieved dynamically by building the ntfs-driver as loadable module (.ko file). This would need to be done for the specific version of the kernel that is running on your device.
Next you need a way to automatically load the module each time the systems restarts. this is also "do-able" in Android. You might want to try this app which does precisely that. i.e. load one or more kernel module(s) located anywhere on the Android device.
After this whenever one inserts a external-device(usb-drive) that has ntfs partitions, the kernel will be able to properly recognise and mount it. Hence apps can then access it at its proper location like "/mnt/usbhost1" etc.

Categories

Resources