0

I am trying to take image from camera on button click and set it in image view on Activity but images not set on image view.I need to set it as a thumbnail. Facing same problem in case of upload.Please resolve it.

This is my code:

private static final int CAMERA_PIC_REQUEST = 1111;


takephoto.setOnClickListener(new 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); 
            }
        });

 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     {
            if (requestCode == CAMERA_PIC_REQUEST) {

                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                iv.setImageBitmap(thumbnail);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
                try {
                    file.createNewFile();
                    FileOutputStream fo = new FileOutputStream(file);
                    fo.write(bytes.toByteArray());
                    fo.close();
                } 
                catch (IOException e) 
                {

                    e.printStackTrace();
                }
            }
     }
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50

3 Answers3

0

Following code will help you:

public class MyCameraActivity extends Activity {
    private static final int CAMERA_REQUEST = 1000; 
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  
    } 
}

Following is xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
        <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

</LinearLayout>

Add in manifest.xml:

<uses-feature android:name="android.hardware.camera"></uses-feature> 
Kinnar Vasa
  • 397
  • 1
  • 9
  • I allready tried this code. bUt still image not set in imageview. I think because of its larger size. I need image as a thumbnail. – Reema Sharma Jul 13 '15 at 09:35
  • You can use code for resize: http://stackoverflow.com/questions/8471226/how-to-resize-image-bitmap-to-a-given-size – Kinnar Vasa Jul 13 '15 at 09:55
0

try this

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            mImage.setImageBitmap(thumbnail);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
            try {
                file.createNewFile();
                FileOutputStream fo = new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

and add this to your manifiest

<uses-feature android:name="android.hardware.camera"></uses-feature> 
Ram Mandal
  • 1,899
  • 1
  • 20
  • 35
0

It can be a big picture size issue. Try this code to reduce the size of your picture.

            Bitmap photo;
            File file = new File(picturePath);
            int file_size = Integer
                    .parseInt(String.valueOf(file.length() / 1024));
            if (file_size > 2048)
            {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 4;
                photo = BitmapFactory.decodeFile(picturePath, options);
            } else if (file_size > 1024)
            {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;
                photo = BitmapFactory.decodeFile(picturePath, options);
            } else
                photo = BitmapFactory.decodeFile(picturePath);
            iv.setImageBitmap(photo);
Bunny
  • 576
  • 4
  • 19