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 113 114 115 116
|
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 com.***.***.R;
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 void getFile(Context ctx, String url) {
if (!getFileFromLocal(ctx, url))
getFileFromNet(ctx, url);
}
/**
* 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
String mime = name.substring(name.lastIndexOf(".")+1); //on recupere l'extension d'un fichier
//On verifie que le repertoire de stockage existe
File files = Environment.getExternalStoragePublicDirectory("test"+File.pathSeparatorChar+"doc");
//Si ce n'est pas le cas, on le créer
if (!files.exists())
files.mkdir();
//On recupere le fichier demandé
File file = new File(Environment.getExternalStoragePublicDirectory("test"+File.separatorChar+"doc"), name); //on récupere le fichier
//On verifie si il existe
if (file.getAbsoluteFile().exists()) {
Log.d(TAG, "file opened");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/"+mime);
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(final Context ctx, final String url) {
String name = url.substring(url.lastIndexOf("/")+1); //on sépare le nom du fichier du reste de l'url
String mime = name.substring(name.lastIndexOf(".")); //on recupere l'extension d'un fichier
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(filePath+url));
request.setDescription(ctx.getResources().getString(R.string.downloading) + " name");
request.setTitle(ctx.getResources().getString(R.string.app_name));
request.setMimeType(mime);
// 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("test"+File.separatorChar+"doc", name);
Log.d(TAG, Environment.DIRECTORY_DOWNLOADS);
// 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);
ctx.unregisterReceiver(this);
}
};
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
ctx.registerReceiver(broadcast, intentFilter);
}
} |
Partager