1

I am using following code in onActivityResult but it is not working

File file1 = new File( Environment.getExternalStorageDirectory().getAbsolutePath(),imagecapture);

            if (file1.exists()) {
              //Do action
                image=(ImageView)findViewById(R.id.image);
             Bitmap myBitmap = BitmapFactory.decodeFile(file1.getAbsolutePath());
          image.setImageBitmap(myBitmap);
       image.invalidate();



            }

But I am not able to get why it is not working I have also used Android Camera Intent: how to get full sized photo? but it gives error on the line mImageUri = Uri.fromFile(photo); Any help will be Appreciated.

Community
  • 1
  • 1
Nitin
  • 1,968
  • 4
  • 22
  • 53

2 Answers2

2
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    Button btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUREST = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnTakePhoto = (Button) findViewById(R.id.button1);
        imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);

        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

          if (requestCode == CAMERA_PIC_REQUEST) {
              Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
              imgTakenPhoto.setImageBitmap(thumbnail);
          }
    }

    class btnTakePhotoClicker implements Button.OnClickListener
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    }
}
Sachin Gurnani
  • 4,421
  • 9
  • 41
  • 48
  • There is no error but some time it is showing but some times it is not showing the image It is really weird – Nitin Jun 05 '12 at 08:00
  • i think it gives you out of memory issue because image size is to big – Sachin Gurnani Jun 05 '12 at 08:02
  • Do you have any solution or I should put another question – Nitin Jun 05 '12 at 08:57
  • You have to compress image size using BitmapFactory.Options option = new BitmapFactory.Options(); option.inSampleSize = 2; bm = BitmapFactory.decodeFile(root.getPath()+"/"+imageName,option); – Sachin Gurnani Jun 05 '12 at 09:33
0
File root=new File(Environment.getExternalStorageDirectory()
                + File.separator + "MYPICS" + File.separator);
File sdImageMainDirectory ;
if (!root.exists()) {
            root.mkdirs();
        }
ImageButton cam =(ImageButton)findViewById(R.id.ImageButton1);
cam.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                imageName = "MyPic" + i + ".png";
                sdImageMainDirectory = new File(root, imageName);

                Uri imgUri = Uri.fromFile(sdImageMainDirectory);

                Intent intent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);

                startActivityForResult(intent, 0);


            }
        });




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
        case 0:
            // finish();
            break;

        case -1:
            files = root.listFiles();
            count = files.length;

            try {
                               BitmapFactory.Options option = new BitmapFactory.Options();
                    option.inSampleSize = 2;
            bm = BitmapFactory.decodeFile(root.getPath()+"/"+imageName,option); 

                   ImageView imageview =(ImageView)findViewById(R.id.imageView1);
                    imageview .setImageBitmap(bm);
    }

            } catch (Exception e) {
                e.printStackTrace();
            }
}
Sachin Gurnani
  • 4,421
  • 9
  • 41
  • 48
  • I am not able to get which class's abject is files – Nitin Jun 05 '12 at 06:57
  • root and sdImageMainDirectory is File Class objects – Sachin Gurnani Jun 05 '12 at 06:59
  • Just debug your application and put a break point at onActivityResult check is your onActivityResult is getting call or not if this happens then your activity is recreated after returning from camera activty because camera activity returns result which we are getting in onActivityResult – Sachin Gurnani Jun 05 '12 at 07:16
  • there is an error on this line files = root.listFiles(); nullpointerexception i think you should give me the declaration of files – Nitin Jun 05 '12 at 07:23
  • that means there is no image saved in path specifid in the file object – Sachin Gurnani Jun 05 '12 at 07:24
  • in the above code which i posted we are created a directory name MYPICS in sdcard in which we are saving image name MyPic.png just check is there any image in this directory or not – Sachin Gurnani Jun 05 '12 at 07:27
  • ya I know that I have also checked for it But there is no such folder and image – Nitin Jun 05 '12 at 07:29
  • Can you give me some tutorial or any other hint – Nitin Jun 05 '12 at 07:32
  • I had not given the write permission in manifest now it has created the folder and image but with the same error on same line – Nitin Jun 05 '12 at 07:38
  • i had posted one more answer let check it it works for you our not because i checked it works fine for me – Sachin Gurnani Jun 05 '12 at 07:41
  • is there an image also in the folder then no way it gives an error and please can you show the through which your opening the camera – Sachin Gurnani Jun 05 '12 at 07:46