簡體   English   中英

如何在 android 應用程序中提示文件管理器進入已知路徑?

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

我的應用程序中有以下活動:

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

我希望在保存文件后能夠提示進入 androids 文件管理器並列出保存的文件。 任何提供的解決方案都告訴我熱到 select 在保存之前的路徑,如本答案或本問題中所見,但我只想在成功保存文件后將文件顯示到設備的文件管理器中。

到目前為止,我開發的是這種方法:

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

作為從按鈕調用的方法,一旦我將文件保存在saveFile方法中。 但是我如何提供意圖的路徑以便顯示?

我只想將文件列出到設備的文件管理器中。

我只想在成功保存文件后將文件顯示到設備的文件管理器中。

抱歉,“在包含特定文件的目錄上打開文件管理器”沒有標准的Intent操作。

到目前為止,我開發的是這種方法

ACTION_GET_CONTENT是允許用戶select一段內容。 如果您不希望用戶這樣做,那么這不是一個合適的Intent操作。

如何在 android 應用程序中提示文件管理器進入已知路徑?

錯誤的問題。

你應該問:

如何讓 ACTION_GET_CONTENT、ACTION_OPEN_DOCUMENT 和 ACTION_OPEN_DOCUMENT_TREE 在預先確定的目錄中打開?

這個問題之前已經回答過了。

訣竅是使用額外的INITIAL_URI

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM