Storage Write on Emulator Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Storage Write on Emulator without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am developing and android application with emulator of Android Studio 3.6. I am using a virtual device of Nexus 7 with andoird 10.

I have given permission on the manifest file and I have asked for the user to give permission for read write to the external storage.

Although permissions are ok I can not create a folder on the external storage. I attempt to write form a background proccess.

This is the excerpt og manifest file

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

This is the excerpt of my Main Activity where I ask the the user to give permission and call the background proccess: UnitDataUpdater

public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    public static final int MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE = 1;
    // public static String SERVER_API_URL =  "http://local.xxxx.xxxxx.eu/index.php?option=com_alumincocc&view=examlist&Itemid=122&format=json&unitCode=";
    // public static LinearLayout examList;
    public JSONArray uiData;
    ListView examsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkPermissions();
        setContentView(R.layout.activity_main);
        String url = "http://xxxxx.xxxxx.eu/index.php?option=com_alumincocc&view=examlist&Itemid=122&format=json&unitCode=478050";
        String result = "";
        examsList = (ListView) findViewById(R.id.examsList);
        UnitDataUpdater process = new UnitDataUpdater(this);
        try {
            result = process.execute(url).get();

        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    ........



    public void checkPermissions() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
                ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super
                .onRequestPermissionsResult(requestCode,
                        permissions,
                        grantResults);

        if (requestCode == MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE) {

            // Checking whether user granted the permission or not.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // Showing the toast message
                Toast.makeText(MainActivity.this,
                        "Write Permission Granted",
                        Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(MainActivity.this,
                        "Write Permission Denied",
                        Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

}

And this is the background process where I try to write to the external storage. The method getAppDi is where I am trying to create a new folder. This folder is never created so the rest of the class throws errors.

A strange thing is that at the emulator I don’t see the Document folder. I have try the same thing on Downloads folder but still the same.

public class UnitDataUpdater extends AsyncTask<String, Void, String> {
    //  private static String REMOTE_URL = "http://local.xxxxxx.xxxxx.eu/index.php?option=com_alumincocc&view=examlist&Itemid=122&format=json&unitCode=";
    private String data = "";
    public static final String REQUEST_METHOD = "GET";
    public static final int READ_TIMEOUT = 15000;
    public static final int CONNECTION_TIMEOUT = 15000;
    private String filePrefix = "i";
    private MainActivity mainActivity;

    public UnitDataUpdater(Activity mainActivity) {
        this.mainActivity = (MainActivity) mainActivity;
    }

     ........

    private String getAppDir() {
        String externalStoragePublicDirectory = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                .getAbsolutePath();


        String appDirPath = externalStoragePublicDirectory + "/XXXXUnitData";
        File appDir = new File(appDirPath);
        if (!appDir.exists()) {
            if (!appDir.mkdir()) {
                try {
                    throw new Exception("Storage Error");
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
        return appDir.getAbsolutePath();
    }
}

Any help is appreciated

Answer

On Android Q external storage is not readable and writable. Use getExternalFilesDir(null) instead. You do not need any read write permission then in manifest or at runtime.

We are here to answer your question about Storage Write on Emulator - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji