gpt4 book ai didi

Android Camera Intent 与 Android L 崩溃

转载 作者:行者123 更新时间:2023-11-30 02:02:36 25 4
gpt4 key购买 nike

我的应用程序在 Kitkat 之前的所有 API 上都能正常工作,但在 Lollipop 中却无法正常工作。当我在我的应用程序中点击使用相机拍摄照片时,它崩溃了。

cam.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});

这是我的 onActivityResult()

    case CAMERA_REQUEST: {

Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picturePath, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
options.inSampleSize = calculateInSampleSize(options,200,100);//512 and 256 whatever you want as scale
options.inJustDecodeBounds = false;
Bitmap yourSelectedImage = BitmapFactory.decodeFile(picturePath,options);

File pngDir = new File(Environment.getExternalStorageDirectory(),"PicUploadTemp");
if (!pngDir.exists()) {
pngDir.mkdirs();
}

File pngfile = new File(pngDir,"texture1.jpg");
FileOutputStream fOut;

try {
fOut = new FileOutputStream(pngfile);

yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 50,fOut);


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable d = Drawable.createFromPath(pngfile.getPath().toString());
rlLayout.setBackground(d);

yourSelectedImage.recycle();
// resizedBitmap.recycle();

xfile = pngfile;
}
break;

我想压缩图像,上面的代码对 gingerbread、ICS、kitkat 的效果非常好,但对 Android L 却不行

出现空指针异常

请给我一些解决方案

提前致谢

最佳答案

如果没有 logcat 输出,真的不能说什么,由于您在 BitmapFacotry 类中使​​用了一个已弃用的方法,它可能会变为 null。

在您的 Activity 中全局定义这些,

   Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

private static final int CAMERA_REQUEST = 1888;

在按钮中点击像这样调用你的相机 Intent

    startActivityForResult(cameraIntent, CAMERA_REQUEST);

确保使用

           @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

用它来检索照片

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == CAMERA_REQUEST && resultCode == getActivity().RESULT_OK) {

//get photo
Bitmap photo = (Bitmap) data.getExtras().get("data");

}

关于Android Camera Intent 与 Android L 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31242967/

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