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?