1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
package com.ylly.hypred.utils;
import android.app.DownloadManager;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
/**
* Created by Altak on 10/08/2015.
*/
public class FileManager {
private static String filePath = "********************";
private static FileManager ourInstance = new FileManager();
public static String TAG = "FileManager";
public static FileManager getInstance() {
return ourInstance;
}
private FileManager() {
}
public File getFile(Context ctx, String url) {
File file = new File("");
if (getFileFromLocal(ctx, url))
return null;
else
getFileFromNet(ctx, url);
return null;
}
/**
* Ouvre un fichier stocké en local
* @param ctx le context de l'application
* @param url l'url du fichier demandé
* @return
*/
private Boolean getFileFromLocal(Context ctx, String url) {
String name = url.substring(url.lastIndexOf("/")+1); //on sépare le nom du fichier du reste de l'url
File file = new File(Environment.getExternalStoragePublicDirectory("test/doc") + File.separator + name); //on récupere le fichier
Log.d(TAG, "test to open " + file.getAbsolutePath());
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
// optionnel a toi de voir quel flag tu souhaites
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
ctx.startActivity(intent);
return true;
}
catch (ActivityNotFoundException e) {
Toast.makeText(ctx,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
return false;
}
}
return false;
}
/**
* Ouvre un fichier téléchargé depuis le serveur
* @param ctx le Context de l'application
* @param url l'url du fihier demandé
*/
private void getFileFromNet(Context ctx, final String url) {
String name = url.substring(url.lastIndexOf("/")+1); //on sépare le nom du fichier du reste de l'url
Log.d(TAG, "Downloading...");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(filePath+url));
request.setDescription("Some descrition");
request.setTitle("Some title");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.getExternalStoragePublicDirectory("test/doc").getPath(), File.separator + name);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
BroadcastReceiver broadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
getFileFromLocal(context, url);
}
};
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
ctx.registerReceiver(broadcast, intentFilter);
}
} |
Partager