Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将画廊图像作为输入流_Java_Android_Image - Fatal编程技术网

Java 如何将画廊图像作为输入流

Java 如何将画廊图像作为输入流,java,android,image,Java,Android,Image,我试图从图库中读取图像,然后使用该图像进行输入扫描,下面是我的代码 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mGetImageButton = (Button) findViewById(R.id.button_getImag

我试图从图库中读取图像,然后使用该图像进行输入扫描,下面是我的代码

@Override
public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mGetImageButton = (Button) findViewById(R.id.button_getImage);
        mGetImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // SET action AND miniType
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setType("image/*");
                // REQUEST Uri of image
                startActivityForResult(intent, REQUEST_IMAGE);
            }
        });
        mImageViewForGallery = (ImageView) findViewById(R.id.imageView2);
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode != Activity.RESULT_OK) {return;}

            if (requestCode == REQUEST_IMAGE) {
                Uri uri = data.getData();
                // SET image
                mImageViewForGallery.setImageURI(uri);
                Drawable drawable = mImageViewForGallery.getDrawable();
                InputStream is;
                is = this.getResources().openRawResource(R.drawable.img1);
                Bitmap bmInImg = BitmapFactory.decodeStream(is);

            }
        }
在上面的代码中,
is=this.getResources().openRawResource(R.drawable.img1)
正在从名为img1的可绘制文件夹读取图像,但现在我的图像是我从gallery中选择的图像,我如何将该图像作为输入流,正如我尝试的那样

InputStream is;
is=uri;
<>但是它的显示错误,对C++的java来说是新的。 编辑,在@Shawn answer之后,我将此代码放在
onActivityResult
函数的第行
Drawable Drawable=mImageViewForGallery.getDrawable()之后

        InputStream is = this.getContentResolver().openInputStream(uri);
        Bitmap bmInImg = BitmapFactory.decodeStream(is);
        InputStream Vign = this.getResources().openRawResource(R.drawable.p);
        Bitmap bmInImg2 = BitmapFactory.decodeStream(Vign);
        mPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
        nPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
        vPhotoIntArray = new int[bmInImg2.getWidth() * bmInImg2.getHeight()];
但是它在
this.getContentResolver().openInputStream(uri)上显示了下面的错误

错误:

Unhandled exception type FileNotFoundException

当我使用
InputStream Vign=this.getResources().openRawResource(R.drawable.p)时出现错误

如果您具有图像的Uri,则可以使用内容解析程序进行解析:

InputStream is = context.getContentResolver().openInputStream(uri);

别忘了关上小溪。(并检查是否为null)。上下文有时是您的活动或“this”。

但是当我使用代码
InputStream Vign=this.getResources().openRawResource(R.drawable.p)在你提到的这一行下面,它会显示错误,否则它不会显示错误,为什么。。。您已经有了输入流,为什么还需要openRawResource?只需使用BitMapFactory对其进行解码是的,但我希望另一个图像将其与inputstream一起使用,inputstream是通过从位图创建bitmapdrawable来绘制的?我会更新我的代码,以及如何在ActivityResult
函数中使用它