Can't move file after DownloadManager completes download - java

in my Android app I am using the system Download Manager to download a photo, once recieved I process it and then I need to move it to another location. if I just copy it it works fine, if I try to move it/delete it I get an exception about it can't be moved.
Failed to delete original file '/data/user/0/com.android.providers.downloads/cache/a3133p2930-9.jpg' after copy to '/data/data/com.XXXXX.testharness/app_appdata/images/one/two/three/6/a3133p2930.jpg'
also tried:
1) remove it from download manager before trying to move it
2) set the destination directly to the target dir, but got an error I can only use public external folders.
3) tried using JAVA renameTo and Apache commons moveFile
this is a code snippet that show the relevant parts of what I am doing.
any ideas?
appreciated
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
String uriString = c.getString(
c.getColumnIndex(
DownloadManager.COLUMN_LOCAL_URI));
String uri2String = c.getString(
c.getColumnIndex(
DownloadManager.COLUMN_URI));
String localFileString = c.getString(
c.getColumnIndex(
DownloadManager.COLUMN_LOCAL_FILENAME));
File from = new File(localFileString);
File to = new File(appDataDir, localString);
File to2 = new File (appDataDir, localString + "/" + getFileNameFromUrl(uri2String));
if (!to.exists()) {
if (to.mkdirs()) {
Log.d(LCAT, "Directory Created");
}
}
dm.remove(downloadId);
//boolean a = from.renameTo(to2);
try {
FileUtils.copyFile(from, to2);
} catch (Exception e) {
Log.d(LCAT, "rename success? " + e.getMessage());
}

i think you can not delete the cache of downloadmanager, i think it clears itself automatically after you removed the downloadid from its database but if you like to have a control to your file you can set destination folder to your private external storage and then after download completed delete it.
File rootDirectory = new File(getActivity().getExternalFilesDir(null).getAbsoluteFile().toString());
if(!rootDirectory.exists()){
rootDirectory.mkdirs();
}
req.setDestinationInExternalFilesDir(getActivity(),null ,"/" + fileName);
and after your download completed you can delete it like:
from.delete();
and the doc says:
public int remove (long... ids)
Cancel downloads and remove them from
the download manager. Each download will be stopped if it was running,
and it will no longer be accessible through the download manager. If
there is a downloaded file, partial or complete, it is deleted.
http://developer.android.com/reference/android/app/DownloadManager.html#remove%28long...%29

Related

Application gives data from deleted database file in Android

I have been working on getting my database backing up to work and I have reached a point where I am not sure what to do.
Basically at first the application opens a Login activity, the user logs in and their database file (if it exists) is downloaded from the Firebase Storage, and then the application navigates to the MainActivity.
In the MainActivity I call a method that sends the user's database file to Firebase Storage. I tried to manage the process by closing the database but since i couldn't fix an error of "E/ROOM: Invalidation tracker is initialized twice :/.", then I found an answer to use a checkpoint (Backup Room database). Now I implemented the forced checkpoint method.
(MarkerDao)
#RawQuery
int checkpoint(SupportSQLiteQuery supportSQLiteQuery);
(MarkerRepository)
public void checkPoint(){
Thread thread= new Thread(() -> markerDao.checkpoint(new SimpleSQLiteQuery("pragma wal_checkpoint(full)")));
thread.start();
}
(ViewModel)
public void setCheckpoint(){
repository.checkPoint();
}
(Database back-up method in the MainActivity)
private void copyDbToFirebase(){
String currentDBPath = "/data/data/"+ getPackageName() + "/databases/locations_table";
File dbBackupFile = new File(currentDBPath);
if (dbBackupFile.exists()){
markerViewModel.setCheckpoint();
// create file from the database path and convert it to a URI
Uri backupDB = Uri.fromFile(new File(currentDBPath));
// Create a StorageReference
StorageReference dbReference = storageRef.child("users").child(userId).child("user database").child("locations_table");
// Use the StorageReference to upload the file
if (userId != null){
dbReference.putFile(backupDB).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.d(TAG, "onSuccess: "+4 + taskSnapshot);
Toast.makeText(getApplicationContext(), "Database copied to Firebase 4", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, "onFailure: "+ e.getMessage());
}
});
}
}
}
If the user logs out, then the files in the "/data/data/"+ getPackageName() + "/databases/" are deleted, which I have manually confirmed by looking at the databases folder of the application.
My issue is that after the databases are deleted and a new user logs in, then the previous database data remains but when I manually check the app's data folder, then the /databases/ folder shows that the files were deleted and a new file is created but it doesn't show any WAL or SHM files and also I get the data of another database which is created when the application first runs, but that file is also not shown in the databases/ folder.
Can anyone explain why the folder doesn't show the files that should be present, where is the application getting the data that was deleted and how to fix it.
Edit: My application has multiple Room databases and I just realized that all the data is still readable after the files were deleted.
The method to delete the database files
private boolean deleteDatabaseFiles(File path) {
if(path.exists() ) {
File[] files = path.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDatabaseFiles(files[i]);
}
else {
files[i].delete();
}
}
}
return true;
}
If you are using the same exact RoomDatabase object simply building another one over the same object will prevent any hold over cached data from showing up. I've tested this using multiple database swaps large and small and there is no bleed over.
If you are using a new Instance of the RoomDatabase object for every login try closing the old one after the user logs out. Room will typically close when not needed but if you need it to happen immediately, manually closing it is your best bet.
roomDb.getOpenHelper().close();

Android Download Manager multiple downloading for links

I created a recording application, user can upload data to google firebase and also can restore the data. That's it, that's all my app does. Now I tested my restoring process with 600 files (mp3 files), then I saw that when I call download manager then the files start downloading but they download one at a time which makes the process very time-consuming. So is there any way to make Download manager to download all the files simultaneously.
void startDownloading(String DownloadUrl, String DownloadPath, String DownloadName) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadUrl));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setTitle("Data");
request.setDescription("Journals");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setDestinationInExternalPublicDir(DownloadPath, DownloadName);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
So I download the files by sending the link, path, and name of the file into the above method, and then task get added to DownloadManager in android device.
How to allow download Manager to download all of them simultaneously as we do in other browsers.
See if you are downloading using download manager then we can download a single file at a time and it depends on the device actually. Many phones have advance download manager but some have a simple manager who can download a single file at a time.
So solution is here instead of calling the download manager we can use this code to download files, it is awesome and always works fine. It downloads all the files simultaneously.
Here is the code:
void startDownloading
(final String DownloadUrl, String DownloadPath, String DownloadName) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
final File file = new File(StringManager.phoneStorageLocation + "/"
+ DownloadPath + "/" + DownloadName);
if (file.exists()) { // decide what to do
} else {
new Thread(new Runnable() {
#Override
public void run() {
try {
URL url = new URL(DownloadUrl);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
} catch (IOException ignored) {
}
}
}).start();
}
}
The above code will download the file in phone storage, Hope it helps someone.

Download a database file to the app directory

i want to update the sqlite database of the app when clicking on a button using the DownloadManager.
But it says "java.lang.IllegalArgumentException: Not a file URI: /data/user/0/com.example.laudien.listviewtesting/databases/Employees"
What do i make wrong? Do i need a permission for that? The internet permission is already in the manifest.
Here is the code of my updateDb() method:
private void updateDb() {
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); // create download manager
DownloadFinishedReceiver receiver = new DownloadFinishedReceiver(); // create broadcast receiver
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); // register the receiver
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DATABASE_URL)); // create a download request
// delete database file if it exists
File databaseFile = new File(getDatabasePath(DATABASE_NAME).getAbsolutePath());
if(databaseFile.exists())
databaseFile.delete();
request//.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN) // not visible
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI) // only via wifi
.setDestinationUri(Uri.parse("file:" + getDatabasePath(DATABASE_NAME).getAbsolutePath())); // set path in app dir
downloadManager.enqueue(request); // enqueue the download request
}
Your path /data/user/0/com.example.laudien.listviewtesting is private internal memory of your app. You obtained it using getFilesDir(). Other apps have no access. Including the download manager. Use external memory given by getExternalStorageDirectory() instead.
For anyone who wants to download sensitive files to app's private folders, I have to say that download manager doesn't have access to these folders inside internal storage.
But the solution is moving the downloaded file in broadcast receiver.
For example putting such line in BroadcastReceiver:
String destinationFileName = "test.mp4";
String filePath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
copyFile(Uri.parse(filePath).getPath(), context.getFilesDir() + "/" + destinationFileName);
and copyFile is a method like this:
private void copyFile(String inFileName, String outFileName) throws IOException {
InputStream myInput = new FileInputStream(inFileName);
OutputStream myOutput = new FileOutputStream(outFileName);
// Transfer bytes from the input file to the output file
byte[] myBuffer = new byte[1024];
int length;
while ((length = myInput.read(myBuffer)) > 0)
myOutput.write(myBuffer, 0, length);
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}

File not found exception? (Voice recog)

Sorry for the long question, I have been stuck on this for a month, and I want to provide as much detail as possible...its just a file not found exception in a simple library... :)
I am getting a file not found exception on my variances file:
I do, however, have the variances file:
I am trying to simply implement voice recognition in my background service, so that I can detect when the user says the word hello (using pocketsphinx).
The problem happens in this method: createSphinxDir();
Here is my service:
#Override
public void onCreate() {
super.onCreate();
setupRecog();
}
private void setupRecog() {
String sphinxDir = createSphinxDir();
Log.v(TAG, "ABOUT TO CREATE SETUP");
if (sphinxDir != null) {
try {
Log.v(TAG, "SETTING UP! :)");
mSpeechRecognizer = defaultSetup()
.setAcousticModel(new File(sphinxDir, "en-us-ptm"))
.setDictionary(new File(sphinxDir, "hello.dict"))
.setBoolean("-allphone_ci", true) //WHAT IS THIS
.getRecognizer();
mSpeechRecognizer.addListener(this);
Log.v(TAG, "ADDED LISTENER");
if ((new File(sphinxDir + File.separator + "command.gram")).isFile()) {
mSpeechRecognizer.addKeywordSearch("hello",
new File(sphinxDir + File.separator + "command.gram"));
Log.v(TAG, "ADDED KEYWORD SEARCH! :)");
}
// Or wherever appropriate
mSpeechRecognizer.startListening("wakeup"); //Is this correct?
Log.v(TAG, "STARTED LISTENING");
} catch (IOException e) {
Log.v("ERROR", TAG);
}
}
}
String createSphinxDir() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String sphinxDir = prefs.getString("sphinx", null);
if (sphinxDir == null) {
Assets assets;
Log.v(TAG, "Assets are not synced, should sync now:");
try {
Log.v(TAG, "In try block!");
assets = new Assets(this);
File sphinxDirFile = assets.syncAssets();
Log.v(TAG, "Syncing assets...should set up listener");
if (sphinxDirFile != null) {
sphinxDir = sphinxDirFile.getAbsolutePath();
SharedPreferences.Editor editor = prefs.edit();
editor.putString("sphinx", sphinxDir);
editor.commit();
Log.v(TAG, "Set up listener");
}else{
Log.v(TAG, "sphinxDirFile is null!");
}
} catch (IOException e) { //THIS IS THE PLACE WHERE I AM GETTING THE ERROR!
e.printStackTrace();
Log.d(TAG, e.toString());
}
}
return sphinxDir;
}
I also have all the call back methods (onPartialResult, onResult, etc.) but they never get called.
Earlier I was getting an exception saying the variances .md5 file didn't exist, so I put a space in between the variances and the .md5, but now I am getting this error, and I don't know why...
Please let me know,
Ruchir
Earlier I was getting an exception saying the variances .md5 file didn't exist, so I put a space in between the variances and the .md5, but now I am getting this error, and I don't know why...
You should not do such things, it causes problems, instead you need to follow the documentation:
The standard way to ship resource files with your application in Android is to put them in assets/ directory of your project. But in order to make them available for pocketsphinx files should have physical path, as long as they are within .apk they don't have one. Assets class from pocketsphinx-android provides a method to automatically copy asset files to external storage of the target device. edu.cmu.pocketsphinx.Assets#syncAssets synchronizes resources reading items from assets.lst file located on the top assets/. Before copying it matches MD5 checksums of an asset and a file on external storage with the same name if such exists. It only does actualy copying if there is incomplete information (no file on external storage, no any of two .md5 files) or there is hash mismatch. PocketSphinxAndroidDemo contains ant script that generates assets.lst as well as .md5 files, look for assets.xml.
Please note that if ant build script doesn't run properly in your build process, assets might be out of sync. Make sure that script runs during the build.
To integrate assets sync in your application do the following
Include app/asset.xml build file from the demo application into your application. Edit build.gradle build file to run assets.xml:
ant.importBuild 'assets.xml'
preBuild.dependsOn(list, checksum)
clean.dependsOn(clean_assets)

How to get the network uri of a file being downloaded from the Download Manager in Android

I am writing an application wherein I want to detect if a download has started and retrieve the URI of the file being downloaded and then cancel the download from the Download Manager. I am doing this so that I can send this URI somewhere else.
The trouble is that I can detect when a download begins by querying the Download Manager, but is there a method or a constant variable in Download Manager from which I can also get the URL of the file being downloaded
Ok its weird answering your own question, but I finally figured out how to do this. There is a DownloadManager class in android.app, which stores a list of all http downloads initiated and their statuses. These can be filtered out based on whether the download is 'RUNNING', 'PENDING', 'PAUSED' and so on.
This list can be read into a cursor and one of the columns of the result is 'COLUMN_URI', which is the url from where the file is being downloaded. A sample code where I have used it is as given below:
public void readDownloadManager() {
DownloadManager.Query query = null;
DownloadManager downloadManager = null;
Cursor c = null;
try {
query = new DownloadManager.Query();
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
//Just for testing I initiated my own download from this url. When an http
// reuest for this url is made, since download is taking place, it gets saved in
// the download manager.
Request request = new Request(Uri.parse("http://ocw.mit.edu/courses" +
"/aeronautics-and-astronautics/16-100-aerodynamics-fall-2005" +
"/lecture-notes/16100lectre1_kvm.pdf"));
downloadManager.enqueue(request);
query.setFilterByStatus(DownloadManager.STATUS_PENDING);
c = downloadManager.query(query);
if(true){
int statusColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int urlColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_URI);
long downloadProcessIdColumnNo = c.getColumnIndex(DownloadManager.COLUMN_ID);
Log.d("Column Count", ((Integer)c.getCount()).toString());
if(c.getCount() > 0){
String url="";
c.moveToLast();
if(c.isLast()){
url = c.getString(urlColumnIndex);
downloadManager.remove(downloadProcessIdColumnNo);
Log.d("Count after remove", ((Integer)c.getCount()).toString());
}
Log.d("After", "Stopped Working");
//Here I am sending the url to another activity, where I can work with it.
Intent intent = new Intent(EasyUploadMainMenu.this, EasyUploadActivity.class);
Bundle b = new Bundle();
b.putString("url", url);
intent.putExtras(b);
startActivity(intent);
Log.d("url:", url);
}
}
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}

Categories

Resources