首页 > 解决方案 > 当我想从存储中读取多个文件时,onActivityResult 怎么样

问题描述

当我想读取多个文件形式存储时,onActivityResult 怎么样

在这种 xml 表单中,我必须通过单击应用程序外部存储中的不同按钮来获取多个图像文件,但它们是 ActivityResult 覆盖方法调用的困难,因为它自动调用并且不能为不同的不同按钮多次调用

它适用于单个文件拾取和获取图像 URI。

那么如何为单个活动中的不同按钮修复 ActivityResult 覆盖方法

公共类 Application_Form 扩展 AppCompatActivity {

ActivityApplicationBinding binding;

boolean isOnlyImageAllowed = true;
private static final int PICK_PHOTO = 1958;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityApplicationBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);
  

    //check user storage permission

    int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                this,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }




    binding.form4.choosePropertyFile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent;
            if (isOnlyImageAllowed) {
                // only image can be selected
                intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            } else {
                // any type of files including image can be selected
                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("file/*");
            }
            startActivityForResult(intent, PICK_PHOTO);
        }
    });



}

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

    if (resultCode == RESULT_OK && requestCode == PICK_PHOTO) {
        Uri imageUri = data.getData();

        binding.form4.selectedPropertyFile.setText("" + imageUri.getLastPathSegment());

    }

}

}

在此处输入图像描述

标签: imagestorage

解决方案


取一个数组来挑选照片,然后通过数组索引检查 if else 语句

公共类 Application_Form 扩展 AppCompatActivity {

ActivityApplicationBinding binding;

boolean isOnlyImageAllowed = true;
private static int[] PICK_PHOTO={0,1,2,3,4,5} ;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityApplicationBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);
    binding.appBar.title.setText("লাইসেন্সের জন্য আবেদন করুন");
    binding.appBar.back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            onBackPressed();
        }
    });


    //check user storage permission

    int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                this,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }


   

    binding.form4.choosePropertyFile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent;
            if (isOnlyImageAllowed) {
                // only image can be selected
                intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            } else {
                // any type of files including image can be selected
                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("file/*");
            }
            startActivityForResult(intent, 0);
        }
    });


    binding.form4.chooseBankCertificate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent;
            if (isOnlyImageAllowed) {
                // only image can be selected
                intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            } else {
                // any type of files including image can be selected
                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("file/*");
            }
            startActivityForResult(intent, 1);
        }
    });



}

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

    if (resultCode == RESULT_OK && requestCode == PICK_PHOTO[0]) {
        Uri imageUri = data.getData();

        binding.form4.selectedPropertyFile.setText("" + imageUri.getLastPathSegment());

    }
    if (resultCode == RESULT_OK && requestCode == PICK_PHOTO[1]) {
        Uri imageUri = data.getData();

        binding.form4.selectedBankCertificate.setText("" + imageUri.getLastPathSegment());

    }

}

}


推荐阅读