Android : Android saving file to device and can't open it

on Sunday, February 1, 2015


so, I'm trying to open a file I save to my device, but the file say that I can't open the file. the device tells me : "This document cannot be opened".


here's my code, the file is .ppt format. thanks a lot !


obviously i have those permissions in the manifest:



<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


here's the code:



String fileName = doc.getName();

String packageName = fragment.getActivity().getPackageName();
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + packageName + "/files/";

File saveFile = new File(path + fileName);

File parentDest = saveFile.getParentFile();
if (!parentDest.exists()) {
parentDest.mkdirs(); //make all the directory structures needed
}
if (!saveFile.exists()) {

try {
saveFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

Toast.makeText(getContext(), "Downloading...", Toast.LENGTH_SHORT).show();

downloadingDocs.add(doc);

final DownloadManager downloadManager;

final long myDownloadReference;

downloadManager = (DownloadManager) fragment.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);

Uri uri = Uri.parse(url);

DownloadManager.Request request = new DownloadManager.Request(uri);

request.addRequestHeader("Authorization", "Basic " + SharedPref.getAuthPrefValue());
request.addRequestHeader("Device", BaseApplication.getCurrentDevice().getDevice().toString());
request.addRequestHeader("DeviceId", BaseApplication.getCurrentDevice().getDeviceId());
request.setDescription(fileName).setTitle("Downloading Document");
request.setDestinationInExternalFilesDir(getContext(), path, fileName);
request.setVisibleInDownloadsUi(true);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);

myDownloadReference = downloadManager.enqueue(request);

finalHolder1.downloadProgress.setVisibility(View.VISIBLE);

new Thread(new Runnable() {
@Override
public void run() {

downloading = true;

while (downloading) {

DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(myDownloadReference);

Cursor cursor = downloadManager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
downloadingDocs.remove(doc);
}
//final double dl_progress = (bytes_downloaded / bytes_total) * 100;
final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

fragment.getActivity().runOnUiThread(new Runnable() {

@Override
public void run() {
finalHolder2.downloadProgress.setProgress((int) dl_progress);
}
});
cursor.close();
}
}
}).start();



} else {

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(saveFile), "application/vnd.ms-powerpoint");

fragment.getActivity().startActivity(intent);
}

0 comments:

Post a Comment