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)时出现错误代码中的code>。如果您具有图像的Uri,则可以使用内容解析程序进行解析:
InputStream is = context.getContentResolver().openInputStream(uri);
别忘了关上小溪。(并检查是否为null)。上下文有时是您的活动或“this”。但是当我使用代码InputStream Vign=this.getResources().openRawResource(R.drawable.p)代码>在你提到的这一行下面,它会显示错误,否则它不会显示错误,为什么。。。您已经有了输入流,为什么还需要openRawResource?只需使用BitMapFactory对其进行解码是的,但我希望另一个图像将其与inputstream一起使用,inputstream是通过从位图创建bitmapdrawable来绘制的?我会更新我的代码,以及如何在ActivityResult
函数中使用它