简体   繁体   English

如何在 android 应用程序中提示文件管理器进入已知路径?

[英]How I can prompt a file manager into a known path in an android app?

I have the following activity in my application:我的应用程序中有以下活动:

public class DisplaySettingsActivity extends AppCompatActivity implements View.OnClickListener {
    
    Button saveIntoFile;
    TextView msg;

    private ActivityResultLauncher<String> requestPermissionLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_settings);
        
        requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
            Log.d("H300s","Permissions Callback");

            if (isGranted) {
                Log.d("H300s","Permission Accepted 2");
                saveFile();
            } else {
                permissionSaveDenied();
            }
        });

        this.saveIntoFile = (Button)findViewById(R.id.save);
        this.saveIntoFile.setOnClickListener(this);
    }

    private void saveFile(){
        Log.d("Η300s","Saving");
        String state = Environment.getExternalStorageState();
        if (!Environment.MEDIA_MOUNTED.equals(state)) {
            Log.e("H300s","Unable to detect external storage");
            saveMsgHandler(null);
            return;
        }

        this.saveIntoFile.setEnabled(false);

        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyMMdd");
        File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        file = new File( file.getAbsolutePath(),"voip_h300s_"+pattern.format(LocalDate.now())+".txt");
        Log.d("H300s",file.toString());
        try {
            file.createNewFile();

            PrintWriter out = new PrintWriter(new FileWriter(file));
            out.println("SOME VALUES");
            out.close();

            saveMsgHandler(file.getAbsolutePath());
        } catch (Exception e) {
            saveMsgHandler(null);
        }
    }

    @Override
    public void onBackPressed() {
        return;
    }

    private void saveMsgHandler(String savePath){
        if (savePath == null) {
            msg.setText(R.string.could_not_save_settings);
            int errorColor = ContextCompat.getColor(this, R.color.error);
            msg.setBackgroundColor(errorColor);
        } else {
            String string = String.format(getString(R.string.save_success),savePath);
            msg.setText(string);
            int success = ContextCompat.getColor(this, R.color.success);
            msg.setBackgroundColor(success);
        }
        msg.setVisibility(View.VISIBLE);
        this.saveIntoFile.setEnabled(true);
    }

    private void permissionSaveDenied(){
        msg.setVisibility(View.VISIBLE);
        msg.setText(R.string.could_not_save_settings);
        int errorColor = ContextCompat.getColor(this, R.color.error);
        msg.setBackgroundColor(errorColor);
        this.saveIntoFile.setEnabled(true);
    }

    @Override
    public void onClick(View v) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Log.d("H300s","Permission Accepted");
            saveFile();
        } else {
            requestPermissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE );
        }
    }
}

And I want once I save the file to be able to prompt into androids file manager and list the saved file.我希望在保存文件后能够提示进入 androids 文件管理器并列出保存的文件。 Any provided solution tells me hot to select a path before saving as seen in this answer or in this question , but instead I want just to show the file into device's file manager after I successfully saving it.任何提供的解决方案都告诉我热到 select 在保存之前的路径,如本答案或本问题中所见,但我只想在成功保存文件后将文件显示到设备的文件管理器中。

So far, what I've developed is this method:到目前为止,我开发的是这种方法:

public void displayFileIntoFileManager(String path){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        startActivity(intent, 7);
}

As a method to call from a button, once I saved the file in saveFile method.作为从按钮调用的方法,一旦我将文件保存在saveFile方法中。 But how I can provide the path to the intent in order to be shown?但是我如何提供意图的路径以便显示?

I just want to list the file into device's file manager.我只想将文件列出到设备的文件管理器中。

I want just to show the file into device's file manager after I successfully saving it.我只想在成功保存文件后将文件显示到设备的文件管理器中。

There is no standard Intent action for "open a file manager on a directory containing a specific file", sorry.抱歉,“在包含特定文件的目录上打开文件管理器”没有标准的Intent操作。

So far, what I've developed is this method到目前为止,我开发的是这种方法

ACTION_GET_CONTENT is to allow the user to select a piece of content. ACTION_GET_CONTENT是允许用户select一段内容。 If you are not looking to have the user do that, then this is not a suitable Intent action.如果您不希望用户这样做,那么这不是一个合适的Intent操作。

How I can prompt a file manager into a known path in an android app?如何在 android 应用程序中提示文件管理器进入已知路径?

Wrong question.错误的问题。

You should have asked:你应该问:

How can i let ACTION_GET_CONTENT, ACTION_OPEN_DOCUMENT and ACTION_OPEN_DOCUMENT_TREE open in a pre determined directory?如何让 ACTION_GET_CONTENT、ACTION_OPEN_DOCUMENT 和 ACTION_OPEN_DOCUMENT_TREE 在预先确定的目录中打开?

That question has been answered before.这个问题之前已经回答过了。

The trick is to use extra INITIAL_URI .诀窍是使用额外的INITIAL_URI

暂无
暂无

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

相关问题 如何在Android File Manager中进行“路径导航”? - How to make “path navigation” in Android File Manager? Android:我如何找到文件路径 - Android: how can i find file path 如何解压缩Android应用中的文件? - How can I untar a file in an android app? 如何在下载管理器中更改文件路径 - Android(Kotlin) - How to change the file path in Download manager - Android(Kotlin) 将文件管理器中的文件路径视为Android意图 - View file path in a file manager as Android intent 我可以从已知文件夹中获取刚刚移动到本地文件系统中某个文件的绝对路径吗? - Can i get the absolute path of just moved file to somewhere in native file system from known folder? 如何在Android的Java应用中添加文件,以便获取路径? - How can I add files to java app in android so I can get it path? 我怎样才能将从 Android 中的下载管理器下载的文件保存到应用程序内部(filesDir),因为现在它不容易访问 Android 中的文件 - How can I save files downloaded from Download Manager in Android to app internal(filesDir) as now its not easy to access files in Android 如何为Android应用程序创建线程管理器? - How do I create a Thread Manager for an Android App? 如何通过 Windows 命令提示符使用 Java Keytool 指定现有 SSL 证书的文件路径? - How do I specific a file path to an existing SSL certificate using Java Keytool via Windows Command Prompt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM