0

I made a class(ShowPhoto.java) which can make and save a photo. But nothing is happening when I click the "V" in my photo maker. So it makes the photo, but when you verify the photo, the app gives an error. When I press this button, the app should save the photo. Any idea what I forgot to add in my code?

package com.example.photoviewer;

//imports here   

public class FotoMaker extends Activity 

    { 
    private static final String LOG_TAG = "debugger";
    ImageView iv;
   // Uri uriOfPicture;

    static final int REQUEST_TAKE_PHOTO = 1;

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
        { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_pic);

        iv = (ImageView) findViewById(R.id.imageView);
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener()

        { 


            @Override 
            public void onClick (View v){
                dispatchTakePictureIntent();
                 Log.i(LOG_TAG, "test5");

            } 
        }); 
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                iv.setImageBitmap(imageBitmap);
            }

        }
    String mCurrentPhotoPath;

    private File createImageFile() throws IOException {
        Log.i(LOG_TAG, "test3");
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }



    private void dispatchTakePictureIntent() {


        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
                Log.i(LOG_TAG, "test");
            } catch (IOException ex) {
                // Error occurred while creating the File
                 ex.printStackTrace();
                 Log.i(LOG_TAG, "test1");
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                Log.i(LOG_TAG, "test2");
            }
        }Log.i(LOG_TAG, "test4");
    }

    }

LogCat:

11-17 21:12:39.960: E/AndroidRuntime(15260): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.keyfinder/com.example.photoviewer.FotoMaker}: java.lang.NullPointerException
11-17 21:12:39.960: E/AndroidRuntime(15260):    at com.example.photoviewer.FotoMaker.onActivityResult(FotoMaker.java:61)
goedkoop
  • 109
  • 1
  • 9
  • Have you set breakpoints/logs to see what is/isn't being called? – codeMagic Nov 17 '14 at 17:54
  • I have actually never worked with breakpoints/logs. I will try to add some logs, and have it sorted out. – goedkoop Nov 17 '14 at 17:55
  • Then you will want to Google "debugging with eclipse" or something similar. You need to learn basic debugging skills or you are in for a long ride. [Here, I got you started](http://www.vogella.com/tutorials/EclipseDebugging/article.html) – codeMagic Nov 17 '14 at 17:56
  • at least Log your try catches to see any exception received. i bet its in your `createImageFile()` you are throwing an exception. Btw its not that hard to perform debugs. – PedroHawk Nov 17 '14 at 17:59
  • Okay thanks guys, I'm working on it. – goedkoop Nov 17 '14 at 18:05
  • My computer sort of crashed, and now my LogCat doenst show anything anymore, while I have no filters at all. But at least I found the error in the deprecated version of LogCat. I put some "log.i(LOG_TAG, "test");" im my code, now I am trying to fix the logcat. EDIT: I fixed the logcat, now im trying to show the log.i's on it. – goedkoop Nov 17 '14 at 18:23
  • Somehow it doesn't show the LOG_TAG's, but I found the error in the logcat. I also edited my question a bit, hope this helps a little bit more. – goedkoop Nov 17 '14 at 18:52
  • It's hard to tell but it looks like whatever is at line 61 of `FotoMaker.java` is `null`. Possibly `iv`or `extras`. It's best to post the logcat output, filtered by "error", instead of a screenshot – codeMagic Nov 17 '14 at 20:11
  • added the logfile above – goedkoop Nov 17 '14 at 20:12
  • Again, what is at line 61? That seems to be the problem. But you should post your entire stacktrace for better help. – codeMagic Nov 18 '14 at 13:57
  • I will update this when I'm home – goedkoop Nov 18 '14 at 14:21

0 Answers0