【问题标题】:How About onActivityResult when I want to read multiple file from storage当我想从存储中读取多个文件时,onActivityResult 怎么样
【发布时间】:2021-04-04 11:30:56
【问题描述】:

要读取多个文件形式存储时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());

    }

}

}

【问题讨论】:

    标签: image storage


    【解决方案1】:

    取一个数组来挑选照片,然后通过数组索引检查 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());
    
        }
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多