Reputation: 745
I'm trying to take a picture and save it into the SD, but at a certain point (after the photo has been taken) I get the NullPointerException. Here's the code:
package com.social.bearv2;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
@SuppressLint("NewApi")
public class Articolo extends Activity {
private static final int CAPTURE_IMAGE_CAPTURE_CODE = 0;
Intent i;
private Button ib;
private ImageView imview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article);
ib = (Button) findViewById(R.id.insert);
imview = (ImageView) findViewById(R.id.imview);
ib.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(i, CAPTURE_IMAGE_CAPTURE_CODE);
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
try {
Toast.makeText(this, "Image Captured", Toast.LENGTH_LONG).show();
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imview.setImageBitmap(imageBitmap);
} catch (NullPointerException ex) {
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "BeAR_" + timeStamp + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(), imageFileName);
return photo;
}
}
Note that the line of code that gives me the exception is the "Bundle extras = data.getExtras();"
And this is the logcat:
03-21 11:41:32.140: E/AndroidRuntime(24225): FATAL EXCEPTION: main
03-21 11:41:32.140: E/AndroidRuntime(24225): Process: com.social.bearv2, PID: 24225
03-21 11:41:32.140: E/AndroidRuntime(24225): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.social.bearv2/com.social.bearv2.Articolo}: java.lang.NullPointerException
03-21 11:41:32.140: E/AndroidRuntime(24225): at android.app.ActivityThread.deliverResults(ActivityThread.java:3385)
03-21 11:41:32.140: E/AndroidRuntime(24225): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3428)
03-21 11:41:32.140: E/AndroidRuntime(24225): at android.app.ActivityThread.access$1300(ActivityThread.java:145)
03-21 11:41:32.140: E/AndroidRuntime(24225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254)
03-21 11:41:32.140: E/AndroidRuntime(24225): at android.os.Handler.dispatchMessage(Handler.java:102)
03-21 11:41:32.140: E/AndroidRuntime(24225): at android.os.Looper.loop(Looper.java:136)
03-21 11:41:32.140: E/AndroidRuntime(24225): at android.app.ActivityThread.main(ActivityThread.java:5081)
03-21 11:41:32.140: E/AndroidRuntime(24225): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 11:41:32.140: E/AndroidRuntime(24225): at java.lang.reflect.Method.invoke(Method.java:515)
03-21 11:41:32.140: E/AndroidRuntime(24225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-21 11:41:32.140: E/AndroidRuntime(24225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-21 11:41:32.140: E/AndroidRuntime(24225): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
03-21 11:41:32.140: E/AndroidRuntime(24225): at dalvik.system.NativeStart.main(Native Method)
03-21 11:41:32.140: E/AndroidRuntime(24225): Caused by: java.lang.NullPointerException
03-21 11:41:32.140: E/AndroidRuntime(24225): at com.social.bearv2.Articolo.onActivityResult(Articolo.java:61)
03-21 11:41:32.140: E/AndroidRuntime(24225): at android.app.Activity.dispatchActivityResult(Activity.java:5423)
03-21 11:41:32.140: E/AndroidRuntime(24225): at android.app.ActivityThread.deliverResults(ActivityThread.java:3381)
Any suggestions?
Upvotes: 0
Views: 266
Reputation: 5871
ACTION_IMAGE_CAPTURE will pass a small Bitmap image to onActivityResult - but only if you don't supply a file URI in EXTRA_OUTPUT.
You have specified where the image file should be stored, so Android assumes you will read the image from that file. This is why the Intent is null in onActivityResult.
Upvotes: 1
Reputation: 4306
if(data != null)
{
Bundle extras = data.getExtras();
System.out.println("extras" + extras);
if (extras != null)
{
bitmap = extras.getParcelable("data");
//setting image resource
imageView.setImageBitmap(bitmap);
}
}
else
{
Log.v("tag" , "data was null");
}
Upvotes: 0
Reputation: 11131
Here the Intent data
is null
...
The stacktrace says that data
is null
...
ResultInfo{who=null, request=0, result=-1, data=null // here}
and you are accessing a null
instance at Bundle extras = data.getExtras();
You will not receive any data when you capture the image as you are passing output location. Your captured image will be saved at the location photoFile
(The location you created)...
Upvotes: 0
Reputation: 951
Take a look in your log:
03-21 11:41:32.140: E/AndroidRuntime(24225): java.lang.RuntimeException:
Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null}
It says data is null. And in your onActivityResult you try to access data:
Bundle extras = data.getExtras();
Upvotes: 0