我用标准相机拍照:
val takePicture = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePicture, TAKE_PICTURE)
接收结果:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK && data != null) {
presenter!!.takeImg(this, data)
}
}
}
以下是函数takeimg
的代码:
fun takeImg(context: Context, data: Intent) {
val imageName = CameraCommon.getFileNamePhotoReg(mImageType)
val bitmap = data.extras!!.get("data") as Bitmap
val imageFile = CameraCommon.createImageByBitmap(context, imageName, bitmap, typePNG)
setImg(imageFile.absolutePath)
}
在某些设备上拍照后发生以下错误:
04-23 17:53:40.153 17393-17393/jp.osaka.mozuyan E/AndroidRuntime: FATAL EXCEPTION: main
Process: jp.osaka.mozuyan, PID: 17393
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=6352, result=-1, data=Intent { dat=content://media/external/images/media/100582 typ= flg=0x1 }} to activity {jp.osaka.mozuyan/jp.osaka.mozuyan.photoreg.PhotoRegActivity}: kotlin.KotlinNullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.access$1400(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:764)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
Caused by: kotlin.KotlinNullPointerException
at jp.osaka.mozuyan.photoreg.PhotoRegPresenter.takeImg(PhotoRegPresenter.kt:46)
at jp.osaka.mozuyan.photoreg.PhotoRegActivity.onActivityResult(PhotoRegActivity.kt:107)
at android.app.Activity.dispatchActivityResult(Activity.java:6490)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
第46行:
val bitmap = data.extras!!.get("data") as Bitmap
和data.extras
为空
为什么extra是null? 如何将数据保存到图像文件。 谢谢。
答案 0 :(得分:1)
使用此代码将图像存储到SD卡中然后只需获取图像并显示图像视图。如果您希望也可以转换为位图
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// takePictureButton.setEnabled(false);
ActivityCompat.requestPermissions(this, new String[]
{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
,Manifest.permission.READ_EXTERNAL_ST ORAGE}, 0);
Toast.makeText(getApplicationContext(),"After permition Restart
App",Toast.LENGTH_SHORT).show();
}else {
takePictureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePicture(v);
}
});
}}
public void takePicture(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = Uri.fromFile(getOutputMediaFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, 100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
imageView.setImageURI(file);
}
}
}
private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "CameraDemo");
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//any path
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}