0

I want to show picture that taken from camera to image view but the size of picture in image view is small, different with the picture that show to image view from gallery, the picture size from gallery is suitable with the screen, anyone can help me to solve this?

Here is my java code

Button btn_kamera = (Button) findViewById(R.id.btnKamera);
    btn_kamera.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                    
                        startActivityForResult(intent, REQUEST_CAMERA);
                    }
                }, 150);
                  }
            });

    Button btn_galeri = (Button) findViewById(R.id.btnGaleri);
    btn_galeri.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent intent = new Intent(
                                Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                        startActivityForResult(intent, RESULT_LOAD_IMAGE);
                    }
                }, 150);
                  }
            });
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

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

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        this._image.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

    else if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK && null != data) {

        this._image.setImageBitmap((Bitmap) data.getExtras().get("data"));

    }

}

and here is my xml code

<?xml version="1.0" encoding="utf-8"?>

           <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/linearLayout1"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:layout_marginBottom="30dp"
                    android:text="Ambil Gambar"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/linearLayout1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="1"
                    android:layout_marginBottom="30dp"
                     >

                    <com.indris.material.RippleView
                        android:id="@+id/btnKamera"
                        android:layout_weight="0.5"
                        android:layout_marginRight="5dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/card_bk"
                        android:gravity="center"
                        android:padding="15dp"
                        android:text="Kamera"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textSize="18sp"
                        ripple:alphaFactor="0.7"
                        ripple:hover="true"
                        ripple:rippleColor="#80D8FF" />

                    <com.indris.material.RippleView
                        android:id="@+id/btnGaleri"
                        android:layout_weight="0.5"
                        android:layout_marginLeft="5dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/card_bk"
                        android:gravity="center"
                        android:padding="15dp"
                        android:text="Galeri"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textSize="18sp"
                        ripple:alphaFactor="0.7"
                        ripple:hover="true"
                        ripple:rippleColor="#80D8FF" />

                </LinearLayout>

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Gambar Belum Dipilih"
                    android:visibility="visible"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:layout_marginBottom="30dp" />


                <com.indris.material.RippleView
                    android:id="@+id/btnProses"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/editText1"
                    android:layout_alignLeft="@+id/editText1"
                    android:layout_marginBottom="30dp"
                    android:background="@drawable/card_bk"
                    android:gravity="center"
                    android:padding="15dp"
                    android:text="Proses"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textSize="18sp"
                    ripple:alphaFactor="0.7"
                    ripple:hover="true"
                    ripple:rippleColor="#80D8FF" />

                <EditText
                    android:id="@+id/editText1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:hint="Teks muncul di sini" 
                    android:editable="false"
                    />


            </LinearLayout>
  • you need to ask for original camera image. Take a look to this:http://stackoverflow.com/questions/6448856/android-camera-intent-how-to-get-full-sized-photo – christian mini Apr 27 '16 at 05:33

2 Answers2

0

We can not judge your code properly.

Can you post your full code??

By the way you can go with very good example of androidhive. check here

To prevent from OutOfMemory Exception for larger image, there is a functionality of BitmapFactory.Options. Using this image should be sampled in size. Check below code snippet

BitmapFactory.Options options = new BitmapFactory.Options();

        options.inSampleSize = 8;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);

        imgPreview.setImageBitmap(bitmap);

Which means your image size is divided in 8th part.

In conclude, make sure you used options.inSampleSize. If yes, change it or if not go through given link.

Harsh Patel
  • 657
  • 11
  • 26
0

try to below code:-

take_image_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <Button 
         android:id="@+id/buttonTakeImageActivity"
         android:layout_width="170dp"
         android:layout_height="60dp"
         android:textSize="25sp"
         android:text="Gallery"/>

       <Button 
         android:id="@+id/buttonTakeImageCam"
         android:layout_width="170dp"
         android:layout_height="60dp"
         android:layout_marginTop="10dp"
         android:textSize="25sp"
         android:text="Camera"/>

    </LinearLayout>

</RelativeLayout>

TakeImageActivity.java

public class TakeImageActivity extends Activity implements OnClickListener {

    File file,mFileTemp;
    Button buttonTakeImage, buttonTakeImageCam;
    ImageView imageView;
    Uri mPhotoUri;
    Bitmap thumbnail;
    private Uri fileUri;
    private static final String IMAGE_DIRECTORY_NAME = "PhotoEditor Camera";
    public static final int MEDIA_TYPE_IMAGE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.take_image_activity);

        file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/FrameImages");

        initialization();
    }

    private void initialization() {
        // TODO Auto-generated method stub
        buttonTakeImage = (Button) findViewById(R.id.buttonTakeImageActivity);

        buttonTakeImageCam = (Button) findViewById(R.id.buttonTakeImageCam);

        buttonTakeImage.setOnClickListener(this);
        buttonTakeImageCam.setOnClickListener(this);

        if (!isDeviceSupportCamera()) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! Your device doesn't support camera",
                    Toast.LENGTH_LONG).show();
            // will close the app if the device does't have camera
            finish();
        }
    }

    private boolean isDeviceSupportCamera() {
        if (getApplicationContext().getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

    public Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        + IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else {
            return null;
        }

        return mediaFile;
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.buttonTakeImageActivity:

            if (Environment.getExternalStorageState().equals("mounted")) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_PICK);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture:"),
                        1);
            }
            break;

        case R.id.buttonTakeImageCam:
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            // start the image capture Intent
            startActivityForResult(intent, 2);
            break;

        default:
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode != RESULT_OK)
            return;

        switch (requestCode) {
        case 1:
            Uri selectedImageUri = data.getData();
            String selectedImagePath = getPath(selectedImageUri);
            Bitmap photo = getPreview(selectedImagePath);
            yourimageview.setImageBitmap(photo);
            break;

        case 2:
                if (resultCode == RESULT_OK) {
                    // successfully captured the image
                    // display it in image view
                    previewCapturedImage();
                } else if (resultCode == RESULT_CANCELED) {
                    // user cancelled Image capture
                    Toast.makeText(getApplicationContext(),
                            "User cancelled image capture", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    // failed to capture image
                    Toast.makeText(getApplicationContext(),
                            "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                            .show();
                }
            break;
        }
    }
        private void previewCapturedImage() {
            try {
                // bimatp factory
                BitmapFactory.Options options = new BitmapFactory.Options();

                // downsizing image as it throws OutOfMemory Exception for larger
                // images
                options.inSampleSize = 2;

                final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                        options);

                yourimageview.setImageBitmap(bitmap);
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

    public Bitmap getPreview(String fileName) {
        File image = new File(fileName);

        BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(image.getPath(), bounds);
        if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
            return null;
        }
        int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
                : bounds.outWidth;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = originalSize / 64;
        // opts.inSampleSize = originalSize;
        return BitmapFactory.decodeFile(image.getPath());
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}
Patel Vicky
  • 766
  • 8
  • 17