Code Monkey home page Code Monkey logo

Comments (3)

nabinbhandari avatar nabinbhandari commented on July 16, 2024

Hello, if you request and get the permission for WRITE_EXTERNAL_STORAGE, the permission READ_EXTERNAL_STORAGE will automatically be granted. If you have further issue, please include an example code which causes the problem.

from android-permissions.

sunbyte avatar sunbyte commented on July 16, 2024

I need WRITE_EXTERNAL_STORAGE permission to download a remote file and READ_EXTERNAL_STORAGE to give the user an option to open the downloaded file. When WRITE_EXTERNAL_STORAGE is allowed by the user, the download process start and the file is downloaded successful. But when I try to open the file, the app crashes.

Here is the code for opening file from storage (works perfect if the READ_EXTERNAL_STORAGE is allowed): https://pastebin.com/jZZvqjJw

from android-permissions.

nabinbhandari avatar nabinbhandari commented on July 16, 2024

I am unable to test your code because it lacks definitions of some methods. However. I did a simple write and read operation on a file and it is working fine.

Please test the following code in your device:

Manifest:

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

MainActivity:

String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE};
Permissions.check(this, permissions, null, null, new PermissionHandler() {
    @Override
    public void onGranted() {
        try {
            File root = Environment.getExternalStorageDirectory();
            File testFile = new File(root, "test.txt");

            FileOutputStream out = new FileOutputStream(testFile);
            out.write("Hello World".getBytes());
            out.close();

            FileInputStream in = new FileInputStream(testFile);
            byte[] data = new byte[11];
            in.read(data);
            in.close();

            String message = "Read: " + new String(data);
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
});

Note that you can simply just request WRITE_EXTERNAL_STORAGE only and it should work fine.

from android-permissions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.