everybody!
I create a file in my android app with this code, run in UI thread:
final File dir = Environment.getExternalStorageDirectory();
final File file = new File(dir, "file" + System.currentTimeMillis()); //$NON-NLS-1$
try
{
file.createNewFile())
}
catch (Exception e)
{
...
}
and I can't see it on my Win7 PC, until I rename the file on the phone with the file browser. I run the program on Samsung Galaxy Nexus, OS ver 4.0.2. What might be wrong?
Thanks.
At last!
The solution is to use MediaScannerConnection.scanFile(context, new String[] {path}, null, null);, but path must include THE NAME OF THE FILE, not only the path. No extension is necessary.
At least, it worked in my case :) .
Huge thanks to Chris Stratton.
The only solution I have found yet, is install SSHDroid on the device and WinSCP on Win7.
Related
I have created a file using
` try {
File myFile = new File("myfile");
if (myFile.exists())
myFile.delete();
outputStream = openFileOutput("myfile", Context.MODE_PRIVATE);
Log.d("MyServiceActivity", "file written");
outputStream.write(User.getInstance().getUserId().getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
`
How do i view the file created manually. Thanks in advance
Since the file is saved with Context.MODE_PRIVATE it is into internal memory, if you dont have root you will be not able to see this file with your phone
The best aproach to view your file is to open device monitor or Android monitor that is place in your Android Studio IDE
Note: run your app in debug mode to be able to see internal memory
directories and files !
Be sure to have your device pluged to your pc to run it
Happy coding
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.
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.
I develop Android app, which uses ciphered obb file. This obb file consists of 3 files (I created it with jobb tool). On the most devices all is ok, but on the three devices there is ERROR_COULD_NOT_MOUNT. And I don't understand how to fix it. I want to distribute app without any errors :) Please, help me to do it! :)
That I have tryed:
1) Unmount and mount obb file;
2) Create obb file from the folder without "read-only" attribute.
But all of it won't work.
Steps to reproduce the problem (including sample code if appropriate).
1) Create folder, put into it 3 different files.
2) Create obb file from the folder from step 1.
3) Try to mount this file from the app (sample code below).
final File mainFile = new File(Environment.getExternalStorageDirectory() + "/Android/obb/" + packageName + "/"
+ "main." + versionCode + "." + packageName + ".obb");
OnObbStateChangeListener listener = new OnObbStateChangeListener() {
#Override
public void onObbStateChange(String path, int state) {
super.onObbStateChange(path, state);
if (state == OnObbStateChangeListener.MOUNTED) {
doNextSteps();
} else if (state == OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT) {
Toast.makeText(getApplicationContext(), "ERROR_COULD_NOT_MOUNT", Toast.LENGTH_LONG).show();
}
}
};
if (!storageManager.isObbMounted(mainFile.getAbsolutePath())) {
storageManager.unmountObb(mainFile.getAbsolutePath(), true, listener);
storageManager.mountObb(mainFile.getAbsolutePath(), "password_string", listener);
} else {
doNextSteps();
}
What happened.
I have "ERROR_COULD_NOT_MOUNT" error on the following devices:
1) HTC PJ401 One S;
2) Samsung GT-I9505 Galaxy S IV;
3) Samsung SM-N9005 Galaxy Note 3.
But on Samsung SM-N900T Galaxy Note 3 and Samsung GT-I9500 Galaxy S IV all is ok.
So, after all my investigations I can say. All I can to do is workaround. And I have created one file from the these 3 files by
copy /B file1+file2+file3 result_file.obb
And then I use this file as non-ciphered obb. After all this manipulations all works well.
I find the reason for some obb file could not be mounted. There a random solt when make obb file, an hashkey generated by PBKDF2WithHmacSHA1. the hashkey from bytes convert to hex string has a bug. I submit a patch to project : platform/framwork/base.
https://android-review.googlesource.com/#/c/230280/
I modified the jobb tool. Add check the hashkey to skip the wrong key generated. Before the Android merge the patch, you can use this patch. This patch also fix the dump files bug.
https://android-review.googlesource.com/#/c/231431/
The scenario I am going for is as follows:
Android application (apk) that is "generic", i.e. I do not want to recompile with different resources
APK will be "pre-installed" on devices and so will not be a "market" app
The application needs a custom configuration file that customizes it with text, etc. that can be different for each installation. (config file needs to be in same location with same name)
On app start-up the config file gets read and configures app according to the data in the config file
In essence, this would be a way to provision the application so that it takes on a specific look and feel based on the config file without having to recompile. Be able to load custom image files, text data, etc.
The problem is that the config file needs to be easily updated and "copied" to a non-rooted device without a SD card. So I need access to the a non-app specific location that is easily accessible via a USB connection and that the APK has access to at run-time. It seems that SharedPreferences and Android file IO is limited to the private app directory under /data/data/pkg/... or external storage. Any ideas would be greatly appreciated. Thanks.
Just thought I would update with at least a partial answer. At least some of my issue has to do with testing in debug mode on my Razr Maxx. When I am connected via USB debugging then the call to create a new file fails as follows:
06-06 10:04:30.512: W/System.err(2583): java.io.IOException: open failed: EACCES (Permission denied)
06-06 10:04:30.512: W/System.err(2583): at java.io.File.createNewFile(File.java:940)
When I am running the app standalone on my device or in an emulator then it all works as expected. Not sure if this is related specifically to Razr Maxx or some other issue?
My code that is working is (from: Write a file in external storage in Android) :
private void writeToSDFile(){
// Find the root of the external storage.
// See http://developer.android.com/guide/topics/data/data- storage.html#filesExternal
File root = android.os.Environment.getExternalStorageDirectory();
mStatusTV.append("\nExternal file system root: "+root);
// See https://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
File dir = new File (root.getAbsolutePath() + "/download");
dir.mkdirs();
File file = new File(dir, "myData.txt");
try {
file.createNewFile();
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found. Did you" +
" add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
mStatusTV.append("Write File 1: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
mStatusTV.append("Write File 2: " + e.getMessage());
}
mStatusTV.append("\n\nFile written to "+file);
}