简体   繁体   English

AndroidStudio getExternalStoragePublicDirectory 在 API 29

[英]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在 API 29 中,不推荐使用 getExternalStoragePublicDirectory ,因此我必须找到将以下代码转换为 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!谢谢你的帮助!

As mentioned in android docs如 android 文档中所述

Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String)通过迁移到 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);
    }

In API 29 and above doing anything with Paths outside of the apps private storage won't work.在 API 29 及更高版本中,对应用程序私有存储之外的路径进行任何操作都将不起作用。

See https://developer.android.com/training/data-storage/files/external-scoped for details有关详细信息,请参阅https://developer.android.com/training/data-storage/files/external-scoped

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");
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM