ghostrider
ghostrider

Reputation: 5259

captured image in android is not fetched from another activity

I am having the following: From my first activity, I am taking an image from the camera and then a new activity opens, where the iage should be previewed and uploaded to the server as a FILE argument in httpPOST request.

In my first activity I am using this code:

public void onClick(View v) {
   String fileName = "temp.jpg";                    
   ContentValues values = new ContentValues();
   values.put(MediaStore.Images.Media.TITLE,fileName);
   mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
   startActivityForResult(cameraIntent,RESULT_LOAD_IMAGE_FROM_CAMERA);
}

and onMyActivityResult I have:

if (requestCode == RESULT_LOAD_IMAGE_FROM_CAMERA
                && resultCode == RESULT_OK && null != data) {
            System.out.println("Photo selected from the camera");

            String[] projection = {MediaStore.Images.Media.DATA};
            //Uri selectedImage = data.getData();

            Cursor cursor = getContentResolver().query(mCapturedImageURI, projection, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            //int columnIndex = cursor.getColumnIndex(projection[0]);
           //file path of captured image
            picturePath = cursor.getString(columnIndex); 
            //file path of captured image
            File f = new File(picturePath);
            String filename = f.getName();

            Toast.makeText(CommunityMain.this, "Your Path:"+picturePath, 2000).show();
            Toast.makeText(CommunityMain.this, "Your Filename:"+filename, 2000).show();
            cursor.close();
       Intent myIntent = new Intent(CommunityMain.this,PhotoPostActivity.class);
       startActivity(myIntent);

The image is saved in the gallery and I do see the picturePath.

When my photoPost activity opens I do have:

f = new File(CommunityMain.picturePath);
        System.out.println("Picture path:"+CommunityMain.picturePath);
        ImageButton image = (ImageButton) findViewById(R.id.photo1);
        image.setImageBitmap(BitmapFactory
                .decodeFile(CommunityMain.picturePath));

The picture path is the same, but my ImageButton is not set.

I do get this:

12-08 17:37:22.670: E/BitmapFactory(13357): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/1386524213331.jpg: open failed: ENOENT (No such file or directory)

but the path is the same with the toast message. What am I missing?

I need to be able also to get the file f since I need that for my HttpPost request.

EDIT: the problem lies in the picturePath. The one I get is different with how the image is being saved? I see a number like the one above, and the actual path inside the photo has something like IMG_2313.jpg for example.

Upvotes: 1

Views: 851

Answers (1)

Snicolas
Snicolas

Reputation: 38168

You should use a different technique. Try to provide the camera with a file url you want the image to be written in like here : https://stackoverflow.com/a/16392587/693752. Next, read that file instead of reading what's provided by the camera.

Upvotes: 1

Related Questions