Reputation: 3746
I take a picture by standard camera:
val takePicture = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePicture, TAKE_PICTURE)
Receiver result:
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)
}
}
}
Following is the code of function 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)
}
Following error occurrs after take picture on some devices:
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)
line 46:
val bitmap = data.extras!!.get("data") as Bitmap
anddata.extras
is null
Why extra is null? How can save data to image file. Thank you.
Upvotes: 0
Views: 246
Reputation: 798
use this code to store image into sd card then just get the image and display the image view.if you want you can convert into bitmap also
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");
}
Upvotes: 1