gpt4 book ai didi

java - 在 Android 上拍照时发生崩溃。为什么?

转载 作者:行者123 更新时间:2023-12-01 05:00:44 24 4
gpt4 key购买 nike

当我用设备拍照时,此代码在 inputStream = 行崩溃,并出现 java.lang.NullPointerException 错误

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uriImage;
InputStream inputStream = null;
ImageView imvCover = (ImageView)this.findViewById(R.id.imvCover);
if ((requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) {
uriImage = data.getData();
try {
inputStream = getContentResolver().openInputStream(uriImage);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, null);
imvCover.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
imvCover.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imvCover.setAdjustViewBounds(true);
}
}

有什么想法吗?

这是我用来打开相机拍照的代码:

    Button btnTakePicture = (Button)this.findViewById(R.id.btnTakePicture);
btnTakePicture.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE);
}
});

最佳答案

试试这个..

    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, "newImage.jpg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, 1);

这是 onActivity 结果..

@Override
protected void onActivityResult(int requestCode, int resultcode, Intent intent)
{

super.onActivityResult(requestCode, resultcode, intent);
File out = new File(Environment.getExternalStorageDirectory(), "newImage.jpg");

if(!out.exists())
{
Log.v("log", "file not found");
Toast.makeText(getBaseContext(),

"Error while capturing image", Toast.LENGTH_LONG)

.show();

return;

}

Log.v("log", "file "+out.getAbsolutePath());
File f = new File(out.getAbsolutePath());

try {

ExifInterface exif = new ExifInterface(out.getAbsolutePath());
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
//Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
Log.v("log", "ort is "+orientation);

} catch (IOException e)
{
e.printStackTrace();
}
Bitmap photo =decodeFile(f,400,400);
}

这是decodeFile函数...

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);

//The new size we want to scale to
final int REQUIRED_WIDTH=WIDTH;
final int REQUIRED_HIGHT=HIGHT;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
scale*=2;

//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}

关于java - 在 Android 上拍照时发生崩溃。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13423945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com