Are you still using a third-party file selector? There are too many bugs. The android getContent official built-in file manager selection adaptation solves the problem that the document type cannot be parsed...

The official file selector is the most powerful in my opinion, but the uri that is brought back needs to be parsed and adapted, and various strange problems will be encountered, such as
selecting from a specific directory or the most recent file, but it cannot be parsed.

B~Z8FW_$@V9_ISYG2M%)HM0.png](https://upload-images.jianshu.io/upload_images/2815884-610db4add44e4e39.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![B~Z8FW_$@V9_ISYG2M%)HM0.png

get down to business

The first is to implement calls anywhere. There is no need to define onActivityResult or call onCreate, registerForActivityResultso I plan to study other methods

At the beginning, I tried to write in onCreate in the fragment, and it would also throw RESUMED and then I couldn’t proceedregisterForActivityResult

So I can only make an empty act, transparent, non-feeling
xml

<activity android:name="FileChooseAct"
            android:launchMode="singleInstance"
            android:theme="@android:style/Theme.Translucent.NoTitleBar">

        </activity>

act

public class TFileChooseAct extends FragmentActivity {
    public static Consumer<Uri> resultCallback;
    private static String input;
    private Handler handler;

    public static void chooseFileByOfficialAPI(FragmentActivity
                                                       activity, String input, Consumer<Uri> resultCallback) {
        FileChooseAct.resultCallback = resultCallback;
        FileChooseAct.input = input;
        Intent intent = new Intent(activity, TranslateChooseAct.class);
        activity.startActivityForResult(intent, 0);//
    }

    Runnable action = new Runnable() {
        @Override
        public void run() {
            TranslateChooseAct.resultCallback = null;
            TranslateChooseAct.input = null;
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(action, 600 * 60);
        ActivityResultLauncher<String> launcher = registerForActivityResult(
                new ActivityResultContracts.GetContent(),
                new ActivityResultCallback<Uri>() {
                    @Override
                    public void onActivityResult(Uri result) {

                        if (resultCallback == null) {
                            Toast.makeText(TranslateChooseAct.this, "选择超时", Toast.LENGTH_SHORT).show();
                            finish();
                            return;
                        }
                        // 处理返回结果
                        setResult(Activity.RESULT_OK);
                        finish();
                        Consumer<Uri> resultCallback1 = resultCallback;
                        resultCallback1.accept(result);
                        TranslateChooseAct.resultCallback = null;
                        TranslateChooseAct.input = null;
                    }
                });
        // 发起调用
        launcher.launch(input);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(action);
        FileChooseAct.resultCallback = null;
        FileChooseAct.input = null;
    }
}

There are some codes above that can be written without writing, for example, considering the possible memory leak problem, because I only think of static when passing parameters, but using static must be destroyed, if it is not called back, it is not a memory leak, although I tested 100% callback, if canceled, the uri is empty and still will be called back.
Usage

chooseFileByOfficialAPI(getThis(), new Consumer<Uri>() {
                    @Override
                    public void accept(Uri uri) {}
);

This is less intrusive, but one more act is created, but brother, there is no way to bypass the detection for convenience.
Finally, the most important thing is to solve the compatibility problem. The popular codes on the Internet are as follows:

おすすめ

転載: blog.csdn.net/u010042660/article/details/130990873