Android: Copying a file from the Assets folder to your mobile memory

Asked

Viewed 390 times

0

I am making an application, which on a button that the user press, will open a PDF file...

There is the problem, this file is in Assets folder and I want to put it in the internal memory of Android...

The problem comes to that, Android 7.0+ is not copying... My current code:

    private void abrirCalendario() {
        AssetManager am = getAssets();
        AssetFileDescriptor afd = null;
        try {
            afd = am.openFd( "calendario_2017.pdf");

            File file = new File(Environment.getExternalStorageDirectory() + java.io.File.separator + "calendario_2017.pdf");
            file.createNewFile();

            copyFdToFile(afd.getFileDescriptor(), file);

        } catch (IOException e) {
            Toast.makeText(m, "Erro! Libere mais espaço no seu armazenamento!", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

        Intent i = new Intent(Main.this, LeitorPDF.class);
        startActivity(i);
    }
    public static void copyFdToFile(FileDescriptor src, File dst) throws IOException {
        FileChannel inChannel = new FileInputStream(src).getChannel();
        FileChannel outChannel = new FileOutputStream(dst).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }

Is there

  • Are you treating the Runtime Permissions at some other point in the app? Or is that just it?

  • That’s just it, the permissions are all right, in versions prior to 7.0 android wheel straight

  • A question, how are you accessing the file in the other Activity? It is yours? Is passing some?

  • I’m sorry to be repetitive, but when I asked you about Permissions, are you treating their request or are you just stating in the manifest?

  • The Activity is mine, and it simply takes the file of that same path in memory and reads... And the permissions are in the manifest

  • 1

    Hmm, do you really need the file in the external storage? pq does not read directly from the Assets in the other Activity? Regarding permissions, as of version 6.0, you not only need to define in the manifest, you need to have the user’s permission to use some features, I recommend taking a look at: https://developer.android.com/training/permissions/requesting.html. Some permissions do not need, but External Storage is one of the dangerous permissions that require user authorization.

  • need to be in storage even... because besides using in Activity I want to save in the card

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.