java - 我想从图库中选择多个图像,但是选择图像后,当我单击“打开”时崩溃

标签 java android image

我想从图库中选择多个图像,但选择图像后,当我单击“打开”时,应用程序崩溃了。

我的代码:

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

        if (resultCode == RESULT_OK) {

            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                Toast.makeText(this, "images pick"+imageUri, Toast.LENGTH_SHORT).show();

                TextView txtview = findViewById(R.id.imguri);
                txtview.setText(String.valueOf(imageUri));

                Intent intent = new Intent(MainActivity.this,ConvertActivity.class);
                intent.putExtra("ImageUri",imageUri.toString());
                startActivity(intent);

               // image_view.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
            }

        }else {
            Toast.makeText(MainActivity.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
        }
    }


///pick gallery code
 public void pickGallery() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
    }

enter image description here

最佳答案

你的 Intent

Intent chooserIntent = new Intent();
chooserIntent.setType("image/*");       
chooserIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
chooserIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(chooserIntent, "Select 
Picture"), 100);
Activity Result

override fun onActivityResult(requestCode: Int, resultCode: Int, data: 
Intent?) {
super.onActivityResult(requestCode, resultCode, data)
   if (requestCode == 100 && resultCode == RESULT_OK && data!=null) {
        val clipdata = data.clipData
        if(clipdata!=null){
            for (i in 0 until data?.clipData!!.itemCount) {
                val uri = data?.clipData?.getItemAt(i)?.uri
                songlist.add(uri)}
        }else{
            val uri = data?.data
            songlist.add(uri)
        }
    }    

关于java - 我想从图库中选择多个图像,但是选择图像后,当我单击“打开”时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59552652/

相关文章:

java正则表达式替换除br之外的所有html标签

android - 是否可以在android中直接将原始PCM编码为Mp4格式

c - 如何在c/matlab中显示8位256x256数组?

java - 在Http Web服务器中基于路径参数过滤请求

java - 代号一应用程序生命周期

java - 从 JOptionPane 下拉列表框中选择后无法刷新 jtextarea

安卓初学者 : Emulator not running app or updating it

类似于 iPhone 的 Android OpenGL 混合

html - 我的背景图片在底部被截断

php - 使用 AJAX 加载图像文件?