I want to open the pdf in associates on Android.

Asked 2 years ago, Updated 2 years ago, 100 views

protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FilefileBrochure=newFile(Environment.getExternalStorageDirectory()+"/"+"abc.pdf");
    if(!fileBrochure.exists())
    {
         CopyAsetsbrochure();
    } 

    /** PDF reader code*/
    File file=new File(Environment.getExternalStorageDirectory()+"/"+"abc.pdf");      

    Intent=new Intent(Intent.ACTION_VIEW);
    int.setDataAndType(Uri.fromFile(file), "application/pdf");
    int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    int.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try 
    {
        getApplicationContext().startActivity(intent);
    } 
    catch(ActivityNotFoundExceptione) 
    {
         Toast.makeText(SecondActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
    }
}

// method to write the PDFs file to sd card
    private void CopyAsetsbrochure() {
        AssetManager assetManager=getAssets();
        String [ ] files = null;
        try 
        {
            files=assetManager.list("");
        } 
        catch(IOExceptione)
        {
            Log.e("tag", e.getMessage());
        }
        for (inti=0;i<files.length;i++)
        {
            String fStr=files[i];
            if(fStr.equalsIgnoreCase("abc.pdf"))
            {
                InputStream in = null;
                OutputStream out = null;
                try 
                {
                  in=assetManager.open(files[i]);
                  out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+files[i]);
                  copyFile(in, out);
                  in.close();
                  in = null;
                  out.flush();
                  out.close();
                  out = null;
                  break;
                } 
                catch (Exception e)
                {
                    Log.e("tag", e.getMessage());
                } 
            }
        }
    }

 private void copyFile(InputStream in,OutputStream out)throws IOException {
        byte [ ] buffer = new byte [1024];
        intread;
        while((read=in.read(buffer))!=-1){
          out.write(buffer,0,read);
        }
    }

It says that abc.pdf cannot be opened by doing so.What should I do?

android pdf

2022-09-30 20:21

1 Answers

I got 2 points from the source

·There are no RuntimePermission related sources, but
 It should be necessary because it is stored in ExternalStorage.
 Have you set up permission and allowed RuntimePermission in the Manifest file?
 Note: Error permission to link cameras using Intent in Android app development?

·Intent delivery via file:// schema is no longer possible from Android 7.0.
 Note: https://developer.android.com/about/versions/nougat/android-7.0-changes.html?hl=ja
 How many versions of Android are there?

Note:

from the source provided Configuring RuntimePermission
and
When I changed the file:// schema to content://, the pdf file was loaded (delivered to other Activity).

·About RuntimePermission
https://developer.android.com/training/permissions/requesting.html

·About file sharing between apps
https://developer.android.com/training/secure-file-sharing/index.html


2022-09-30 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.