How do I know if a file can be deleted by my android application?
I am writing an android application and a web application that will allow my clients to view and delete their files.
But I will not let the client to view the files that are undeletable.
Like
if(isFileCanBeDeleted("filename.ext"))
{
//save the filename and path to server
}
else
{
//the file is cannot be deleted, this filename will not be saved online
}
You can try by executing shell command,
Runtime.getRuntime().exec("test -e " + "/path/to/file.txt");
However, you need to check API references whether shell runtime is available or not, depending on API Level, or root/non-rooted phone.
Related
I currently have a Qt Application that writes to a log during use and saves the file locally to the device.
This is straightforward enough, I create a QFile with the local path and write to it.
It looks something like this:
QFile logFile;
QString logPath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
logFile.setFileName(logPath.append("/MyLogFile.txt"));
logFile.open(QFile::WriteOnly | QFile::Append);
// Write to log file ...
This works fine but I'd like to also write this log file to a network drive -
E.g. //192.168.12.34/Drive-1/LogFile.txt
I've tried altering the path to //192.168.12.34/Drive-1
but see nothing new in the network folder when I navigate to the above on a desktop.
Is this something that's possible to do from an Android device?
It can't be done this way. You need to mount the network drive first using a samba client, and then copy the files.
You can use a samba client app like AndSMB thats allow folder synchronization for that.
My application will be used offline and I plan daily pull/push synchronizations via USB cable. My users are in a very primitive situation: no wifi, and no cell phone towers. My question is not about synchronization, but rather just getting access to the data so that I can synchronize.
I connect the cable, select USB for file transfer, and I can see Internal Shared Storage. But I cannot find my SqlLite database anywhere. I have tried using these paths for the database:
Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
Environment.GetFolderPath(Environment.SpecialFolder.Personal);
Android.App.Application.Context.FilesDir.AbsolutePath;
The app works fine with any of those paths, the data is stored and retrieved, but I cannot see the database from my PC.
I have also tried this but it blows up:
Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
I have tried debugging with Xamarin Live but then I get this error:
"You need to call SQLitePCL.raw.SetProvider();"
I have tried adding console.writeline and Log.Error to add in some diagnostics but I can't find any log files in Internal Shared Storage.
I have WRITE_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions in the manifest; even tho I think that is not necessary.
If I could store files in Internal Shared Storage then I could put the database there and access it to synchronize. And I could create a simple text logging facility to write a text log to the same place.
I have rarely asked for help in 40 years but I've been at this for days. Thanks!
To get files onto Internal Shared Storage accessible via USB took 3 steps:
1: Get runtime permissions
ActivityCompat.RequestPermissions(activity, new String[] { Manifest.Permission.WriteExternalStorage, Manifest.Permission.ReadExternalStorage }, 1);
2) use this path:
string extPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
extPath = Path.Combine(extPath, "RtTrace.txt");
File.AppendAllText(extPath, "new content" + System.Environment.NewLine);
3) Media scan the resulting file to make it visible:
MediaScannerConnection.ScanFile(Android.App.Application.Context, new String[] { extPath }, null, null);
The path on Environment.SpecialFolder.Personal refers to a private area where only the app (and the OS itself when you clear data from the app, for example) have access. I don't know about ApplicationData.
You can easily copy your app file (the protected one) to a public folder, like Downloads or create a new folder MyAppDirectory at the public storage space, that will allow access from other devices. Then, you can clear local data that you don't need anymore (after the sync process).
To create a new public folder on Android:
var folder = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "MyAppDirectory");
System.IO.Directory.CreateDirectory(folder);
You'll get this:
Then, copy the file:
var newFile = Path.Combine(folder, "MySharedFile"); // The database, xml, json, text or any file you want to share
var sourceFullName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),"MyOriginalFile");
System.IO.File.Copy(sourceFullName, newFile, true /* overwrite */);
I hope it helps.
SQLite db is stored in application private memory and can't be accessed over USB, try exporting contents of your DB to an external file in your internal memory or External memory to be able to access it via a USB chord.
I am trying to access external storage on my Android App in order to access a log file so I created:
new File(getExternalFilesDir(null),"log.txt");
And then created this file in the root of my Android device.
To test the existence of the file I ran:
if (log!=null)
{
return log.exists();
}
return false;
But regardless of the files presence or lack thereof it returns false.
To find the location of where I was referencing I ran:
System.out.println(log.getAbsolutePath());
Which returns
/storage/emulated/0/Android/data/com.supportmeplus.betatester/files/log.txt
Where is this? I'd just like to access a log file that can be accessed from outside of the app later on (on a computer). Does this have to do with the fact that i'm test-running the app with an emulator?
Thanks
Is it possible to search through a currently open developer's app (ie not signed) via adb?
I can currently use adb shell to sniff around inside the phone. But I have no idea if I can look at the current app that is open in the phone.
[edit]
Just for some background information, in my app I check to see if a file exists or not, this way I can tell if the app is live or just in development. As when the app is built for live different files are added.
So I use this:
$.ajax({
url:'file:///assets/www/cordova.js',
type:'HEAD',
error: function(el)
{
c(el.readyState);
},
success: function()
{
c('//file exists');
}
});
This will check if the file exists or not, however recently the files used have changed and I can no longer find them.
I have a product using Android 2.3, and my app is put into /system/app/.
Now when my app find new version, my app try to download the .apk(updated version) file to /system/app/ , but failed. I couldn't create file in /system/app/.
So, how could I update my app locate in /system/app?
(I have all source code of Android+Linux kernel+uboot of my product.)
piece of code:
case WdtMessage.UPDATE:
Log.i(TAG, "======================update");
FileOutputStream out = null;
try {
out = new FileOutputStream("/system/app/dddddddd.apk");
out.write((updateFileBuffer));
}catch (Exception e) {
// TODO: handle exception
Log.i(TAG, "======================err"+e.toString());
}
create file Permission denied!
Google's new policy doesn't allow apps in the Play Store that try to update themselves. Updates must be passed through the Play Store.
If you're not looking to put your app up in the store, as far as I know, you wont be able to overwrite the APK (without root access).
PS : Facebook recently did something similar with their application, if you really need this, try researching how they did it.
EDIT:
For your requirements, you will need root access. So the device you're developing on needs to be rooted. With that in mind , you could use RootTools to access the /system/app/ directory.