Hello. I want to get the image in the code in the form of a bitmap, after something I photograph.

here is my code:

static final int PHOTO_REQUEST_CODE = 1; private Uri outputFileUri; private String img_path = Environment.getExternalStorageDirectory().toString() + "/MyApp/imgs/ocr.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button captureImg = (Button) findViewById(R.id.action_btn); captureImg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startCameraActivity(); } }); } private void startCameraActivity() { outputFileUri = Uri.fromFile(new File(img_path)); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent, PHOTO_REQUEST_CODE); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK && intent.getData() != null && requestCode == PHOTO_REQUEST_CODE) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 4; Bitmap bitmap =BitmapFactory.decodeFile(outputFileUri.getPath(), options); // ТУТ Я ХОЧУ РАБОТАТЬ С ЭТОЙ ФОТОГРАФИЕЙ. } else { Toast.makeText(this, "ERROR: Image was not obtained.", Toast.LENGTH_SHORT).show(); } } 

But, I get the following error.

 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.organization.tesseracttest, PID: 29212 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.organization.tesseracttest/com.organization.tesseracttesttest.MainScreen}: java.lang.NullPointerException at android.app.ActivityThread.deliverResults(ActivityThread.java:3574) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) at android.app.ActivityThread.access$1400(ActivityThread.java:169) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5476) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.organization.tesseracttesttest.MainScreen.onActivityResult(MainScreen.java:95) at android.app.Activity.dispatchActivityResult(Activity.java:5643) at android.app.ActivityThread.deliverResults(ActivityThread.java:3570) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) at android.app.ActivityThread.access$1400(ActivityThread.java:169) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5476) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method) Disconnected from the target VM, address: 'localhost:8604', transport: 'socket' 

I somehow, in principle, do not understand why he swears. Can you explain the reason why this happens, and how to correct the error?

  • Have you registered permission to use the camera and memory in the manifest? - Arnis Shaykh
  • in the manifest: <uses-android: name = "android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-android: name = "android.permission.CAMERA" /> <uses-feature android: name = "android.hardware .camera "android: required =" true "/> - fantastic
  • You also need this <uses-feature android: name = "android.hardware.camera" android: required = "true" /> You also need to check the switches themselves in the application settings. - Arnis Shaykh
  • Check the update. - Arnis Shaykh

1 answer 1

Found a solution here https://stackoverflow.com/questions/20536603/null-pointer-after-capturing-image-using-android-camera . The error is the same, so that should help. I understand that the fact is that the data is lost when moving from the camera back to the application, so a nullpointerexception crashes.

Update

If you follow the link . So you can learn how to do everything you need. I did and it works for me. The problem here is that you need a content provider to transfer the file from the camera application to your application.

  • Thanks for the answer. However, this decision did not help me. I keep catching Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo {who = null, request = 1, result = -1, data = null}. - fantastic
  • Do you need a full-sized photo or miniature? - Arnis Shaykh