1

So far I have my app taking a picture creating a new folder on the SD Card and saving the pictuers into the new folder.

I'm trying to get it so once the picture has been took it will display in a new Activity with two buttons that say "Use" or "Retake". So far the image saving is working perfectly fine but once the image has been took and it tries to open the new Activity it just stays on the camera Activity and shows the image which I cant use as it has a surfaceView onit.

In my LogCat I get the error "Oh, no reference" which is set to show if it can't find the picture, which is why im thinking it may be because I am not calling the picture from the correct place in my Punch.java.

So basiclly I am trying to once an image has been took the app to open a New Activity "Punch.java" and display the image that has just been took.

UPDATE Thanks to Lumis (code below has been updated)

Changed

intent.putExtra("filepath",uriSavedImage);

to

intent.putExtra("filepath",uriSavedImage.toString());

Which now opens the new Activity but still cannot see the image.

UPDATE 2 Punch.java

I have updated my Punch.java as with the new code if i change (myRef) to "/sdcard/Punch/image_0.jpg" I can see that image but I need it to referance to the image that was just taken with the camera which is something to do with this line I think intent.putExtra("filepath",uriSavedImage.toString());

Update 3

Nearly working perfectly now using intent.putExtra("filepath",Uri.parse(output.getAbsolutePath()).toString()); but for some reason it is still putting mnt/sdcard at the start it just needs to be sdcard/

Ok now working fine /mnt/sdcard is when the sdcard was mounted to the computer while i took the picture.

In my Camera Activity I have

PictureCallback myPictureCallback_JPG = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        /*Bitmap bitmapPicture 
            = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */
        int imageNum = 0;
        Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
        imagesFolder.mkdirs(); // <----
        String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        File output = new File(imagesFolder, fileName);
        while (output.exists()){
            imageNum++;
            fileName = "image_" + String.valueOf(imageNum) + ".jpg";
            output = new File(imagesFolder, fileName);
        }

        Uri uriSavedImage = Uri.fromFile(output);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);


        OutputStream imageFileOS;
        try {
            imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(AndroidCamera.this, 
                    "Image saved", 
                    Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Intent intent = new Intent(getBaseContext(), Punch.class);
        intent.putExtra("filepath",uriSavedImage.toString());
        //just using a request code of zero
        int request=0;
        startActivityForResult(intent,request); 
    }};

And my Punch.java which is the next Activity is:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.punch);

    String myRef = this.getIntent().getStringExtra("filepath");

    File imgFile = new  File(myRef);
    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        ImageView myImage = (ImageView) findViewById(R.id.imagepunch);
        myImage.setImageBitmap(myBitmap);

    }

}

}

Matt
  • 1,747
  • 8
  • 33
  • 58
  • I think mnt is normal, check the answer for this question: http://stackoverflow.com/questions/7102104/android-how-to-access-a-file-in-the-sd-card It should be whatever Environment.getExternalStorageDirectory() returns... – Lumis Jan 04 '12 at 11:21

2 Answers2

1

I think it is the file path issue. You submitted your file path as URI but you are reading it in the viewer activity as a string.

Perhaps you neet to change this line into:

intent.putExtra("filepath",uriSavedImage.toString());

Or

intent.putExtra("filepath",Uri.parse(output.getAbsolutePath()).toString()); 

Different version of android may not work the same when it comes to file path, so you need to experiment using Uri.parse(fileStr) or String...

Lumis
  • 21,517
  • 8
  • 63
  • 67
  • I updated the line of code to your answer above which now takes me to the new Activity but still cannot see the image? – Matt Jan 03 '12 at 15:20
  • Check if the image is on the SD card inthe right folder. If it is then something is wrong with the file path you are using in decodeFile. Try to manually type the path which works in decodeFile to see its format. – Lumis Jan 03 '12 at 15:30
  • Not sure how you mean to do this you mean in the Punch.java instead of (myRef)? – Matt Jan 03 '12 at 16:03
  • please see **Update 2** in my question – Matt Jan 03 '12 at 16:10
  • That is good now just do Log.e for your myRef and compare the path. – Lumis Jan 03 '12 at 16:14
  • Not really used Log.e that much not sure how to implement that any suggestions? – Matt Jan 03 '12 at 16:19
  • In Punch Java just add Log.e(">>>", myRef); to see what myRef actually is. Then look into LogCat in DDMS after you take the photo – Lumis Jan 03 '12 at 16:26
  • You can filter LogCat by clicking E button, and this is better way than using Toast. – Lumis Jan 03 '12 at 16:28
  • Ok in my LogCat it gives me `file:///mnt/sdcard/Punch/image_20.jpg` which is the correct image name which has been took but i just dont think the correct path which is why its not showing. Should it not just be `/sdcard/Punch/image_20.jpg` – Matt Jan 03 '12 at 16:40
  • Since you know now the correct path, you can only pass directory and the image name and construct a working path in Punch activity. That should solve it. But test this on different versions of android exspecially on on 2.1 and below. – Lumis Jan 03 '12 at 17:11
  • You can also try: intent.putExtra("filepath",Uri.parse(output.getAbsolutePath()).toString()); – Lumis Jan 03 '12 at 17:27
  • Nearly working perfectly now using `intent.putExtra("filepath",Uri.parse(output.getAbsolutePath()).toString());` but for some reason it is still putting `mnt/` at the start it just needs to be `sdcard/` – Matt Jan 04 '12 at 09:23
0

Just looking at your code and your approach, I think what you would typically see in this scenario is a new content handler registered so that it appears under the "Share" option of the camera / image library. This way, you are not getting involved in the use/retake logic which is essentially redundant for built-in Android functionality. Think of an app like Evernote or Picasa. You take a picture (or look one up that you took previously) and select "Share". One of the Share options, along with Picasa, Email, etc would be your app. That is how I would do it.

Wade
  • 44
  • 1
  • I did think about this but for the app to work it has to have the picture took in a certain way so it has to use a custom camera with a surfaceView – Matt Jan 03 '12 at 14:52