fekra fekra - 2 months ago 15
Android Question

open pdf after downloaded it

I use this code to download pdf

downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("story" + name);
request.setDescription("download " + name);
request.setDestinationInExternalFilesDir(R_arabic.this,"/Rewayat/", name+".pdf");
Long reference = downloadManager.enqueue(request);


it work good and pdf downloaded in app folder in android/data

and i use this code to open pdf

File file = new File ("/data/com.kamal.ahmed.rewaya/files/Rewayat/"+name+".pdf");
Intent target = new Intent (Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createchosser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
}


but when i try to open it i don't get right pdf file and get error file
i think path not right please help me

A.J A.J
Answer

To download the document to external storage

DownloadManager.Request r = new DownloadManager.Request(Uri.parse(document_url));

// This puts the downloaded document in the Download directory
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my_document.pdf");  
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);        
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(r);

Code to open

public void OpenPDF() {
    try {
        File file = new File(Environment.getExternalStorageDirectory()
                + "/Download/" + "my_document.pdf");
        if (!file.isDirectory())
            file.mkdir();
        Intent pdfIntent = new Intent("com.adobe.reader");
        pdfIntent.setType("application/pdf");
        pdfIntent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        pdfIntent.setDataAndType(uri, "application/pdf");
        startActivity(pdfIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

You need permission to write external storage, check how to get permission in new android version (you have to request permission through code, not only in the manifest)

To request permission from the user in order to write external storage, place these two methods in your main activity

public  boolean isStoragePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                Log.v("PERMISSIONS", "Permission is granted");
                return true;
            } else {

                Log.v("PERMISSIONS","Permission is revoked");
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                return false;
            }
        }
        else { //permission is automatically granted on sdk<23 upon installation
            Log.v("PERMISSIONS","Permission is granted");
            return true;
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
            Log.v("PERMISSIONS","Permission: "+permissions[0]+ "was "+grantResults[0]);
            //resume tasks needing this permission
        }
    }

and inside onCreate, call the method

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ...
        isStoragePermissionGranted();

Inside manifest.xml before tag put these two permissions

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