Vu Nguyen
Vu Nguyen

Reputation: 31

AndroidStudio getExternalStoragePublicDirectory in API 29

In API 29, getExternalStoragePublicDirectory was deprecated so that I have to find way to convert the following code to API 29

String pathSave = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                            + new StringBuilder("/GroupProjectRecord_")
                            .append(new SimpleDateFormat("dd-MM-yyyy-hh_mm_ss")
                            .format(new Date())).append(".3gp").toString(); 

Thanks for your help!

Upvotes: 2

Views: 902

Answers (2)

Andrew
Andrew

Reputation: 10162

In API 29 and above doing anything with Paths outside of the apps private storage won't work.

See https://developer.android.com/training/data-storage/files/external-scoped for details

So to save you need to do something like:-


    // OnClick Save Button
    public void Save(View view){

        // Ask for a new filename
        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        // Restrict to openable items
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        // Set mimeType
        intent.setType("text/plain");
        // Suggest a filename
        intent.putExtra(Intent.EXTRA_TITLE, "text.txt");
        // Start SAF file chooser
        startActivityForResult(intent, 1);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        super.onActivityResult(requestCode, resultCode, resultData);

        if (requestCode == 1 && resultCode == RESULT_OK) {
            Log.d("SAF", "Result code 1");
            if (resultData != null) {
                Uri uri = resultData.getData();
                Log.d("SAF", uri.toString());

                // Now write the file
                try {
                    ParcelFileDescriptor pfd =
                            this.getContentResolver().
                                    openFileDescriptor(uri, "w");

                    // Get a Java FileDescriptor to pass to Java IO operations
                    FileDescriptor fileDescriptor = pfd.getFileDescriptor();

                    // Read Input stream
                    FileOutputStream fileOutputStream =
                            new FileOutputStream(fileDescriptor);

                    // .....

                } catch (Exception e){
                    // Do something with Exceptions
                }
            } else {
                Log.d("SAF", "No Result");
            }
        }
    }

Upvotes: 0

Jeeva
Jeeva

Reputation: 1951

As mentioned in android docs

Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String)

Try with this method.

public void getFilePath(Context context){
        String path = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
                + new StringBuilder("/GroupProjectRecord_")
                .append(new SimpleDateFormat("dd-MM-yyyy-hh_mm_ss")
                        .format(new Date())).append(".3gp").toString();

        Log.d(TAG, "getFilePath: "+path);
    }

Upvotes: 2

Related Questions