2

I have implemented Camera via Intent and set that Image in Imageview which is appearing blurred, while the same Image appear's clear in ImageGallery. The code and related picture are as follows :

Image saved in SD CARD

Blurred image in Imageview

Activity Class :

   public class MainActivity extends ActionBarActivity {
   Button b1,b2;
   ImageView iv;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      b1=(Button)findViewById(R.id.button);
      iv=(ImageView)findViewById(R.id.imageView);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);
         }
      });
   }

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

      Bitmap bp = (Bitmap) data.getExtras().get("data");
      iv.setImageBitmap(bp);
   }

File is being saved in sdcard and also I retrive its URI sucessfully, but still imageview do not show this image or imageview disappear when camera click and return. Code are as follows -:

public class MainActivity extends Activity {

    static int TAKE_PIC =1;
    Uri outPutfileUri;
    ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.image);
    }


    public void CameraClick(View v) {

        Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory(),
                "MyPhoto.jpg");
        outPutfileUri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
        startActivityForResult(intent, TAKE_PIC);
    }

    Bitmap bitmap = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent data)
    {
        if (requestCode == TAKE_PIC && resultCode==RESULT_OK) {

            String uri = outPutfileUri.toString();
            Log.e("uri-:", uri);
             mImageView.setImageURI(Uri.parse(uri));
        }
}}

Please suggest me how to get path of clicked image and set it to imageview. I am new here.

Akash
  • 123
  • 2
  • 5
  • 13

2 Answers2

4

First of all, the image is blurred because the intent data contains only the thumbnail of the image.

you must save the image on the internal/external storage and then acquire it.

feel free to take a look at the android developers documentation regarding camera images:

Android Developers

and this older SO answer explains how to retrieve the full size image after saving it :

Android get full size image from camera

Community
  • 1
  • 1
CptEric
  • 897
  • 9
  • 23
  • if the app crashes, copy the stackstrace to your question so we can see it. – CptEric Dec 03 '15 at 10:45
  • you are trying to get the uri from the "data" thumbnail, that will never work. you must follow the android developers tutorial i linked to correctly save on a accessible directory and, on capture result, access the directory and retrieve it. – CptEric Dec 03 '15 at 10:56
  • a single line won't make all the work the rest of the code in this, http://developer.android.com/intl/es/training/camera/photobasics.html , previously linked tutorial. you must prepare the camera first, and after it's retrieved, decode it. – CptEric Dec 03 '15 at 10:59
3

Try This,

public class MainActivity extends Activity {

    static int TAKE_PIC =1;
    Uri outPutfileUri;
    ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.image);
    }


    public void CameraClick(View v) {

        Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory(),
                "MyPhoto.jpg");
        outPutfileUri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
        startActivityForResult(intent, TAKE_PIC);
    }

    Bitmap bitmap = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent data)
    {
        if (requestCode == TAKE_PIC && resultCode==RESULT_OK) {

            String uri = outPutfileUri.toString();
            Log.e("uri-:", uri);
            Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();

            //Bitmap myBitmap = BitmapFactory.decodeFile(uri);
           // mImageView.setImageURI(Uri.parse(uri));   OR drawable make image strechable so try bleow also

            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outPutfileUri);
                Drawable d = new BitmapDrawable(getResources(), bitmap);
                mImageView.setImageDrawable(d);
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
}

}

Hope this will help you enjoy!!!!!

Akash kumar
  • 981
  • 3
  • 14
  • 27