Well, this is obviously a recurring question, but in all the rest I have not found a solution that works ...
So, I'm trying to take a picture with the camera and show this image in the image view ... At the moment, it doesn’t matter if it is effective or something, so I do not deal with this ...
Here is my code:
Launch Camera:
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePicture.resolveActivity(getPackageManager()) != null) {
File image = null;
try {
image = createImageFile();
} catch (IOException ex) {
finish();
}
if(image != null) {
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
startActivityForResult(takePicture, 1);
}
}
}
});
createImageFile:
private File createImageFile() throws IOException {
String name = System.currentTimeMillis() / 1000 + "";
File dir = getExternalFilesDir(null);
File image = File.createTempFile(name, ".png", dir);
path = "file:" + image.getAbsolutePath();
return image;
}
and onActiviyResult:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && requestCode == 1) {
String path = data.getExtras().getString(MediaStore.EXTRA_OUTPUT);
File f = new File(path);
Bitmap b = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(f.getAbsolutePath()), 50, 50, true);
iv.setImageBitmap(b);
} else {
}
}
It is important to note that my application never reaches the method onActivityResult, but gets stuck somewhere after I take the picutra and click “Save” to save the image (this screen is displayed after the image has been taken, this is not what I implemented, but this is how it is on Android)
Here is my logcat:
04-08 14:57:37.230 5976-5976/hr.fer.dummyproj E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {hr.fer.dummyproj/hr.fer.dummyproj.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3500)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
at android.app.ActivityThread.access$1200(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at hr.fer.dummyproj.MainActivity.onActivityResult(MainActivity.java:84)
at android.app.Activity.dispatchActivityResult(Activity.java:5563)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
at android.app.ActivityThread.access$1200(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
UPDATE
, , . , , . , onResultActivity .
, Uri onResultActivity picutre