cant even take a picture and show it in an imageview!

All your problems with Audio, Video and Images.

cant even take a picture and show it in an imageview!

Postby cargo100 » Wed Dec 15, 2010 12:24 pm

Can someone please please give me working code to solve the following problem:

My problem: I want to take a picture from my application with the native camera app and show it in an imageview.

I have followed so many tutorials but it always comes down the the two following issues:
1) In onActivityResult the Data intent is null and thus I cant use intent.getData() even though I have specified MediaStore.EXTRA_OUTPUT when I start the camera intent.

2) Using an Uri to store the location of the image and using it in the imageView only works if the picture has been taken in landscape orientation. If I take the picture in portrait orientation the imageview shows nothing.

I get these problems using a Samsung Galaxy S and also a Samsung Galaxy Tab. If I use a HTC Desire I do not have problems. They all use 2.2.

This is the code I am using, it is very short and basic and I really need help here:

Code: Select all
   Uri photoUri = Uri.EMPTY;

   public void onTakePictureClick(View v)
   {
      ContentValues values = new ContentValues();
        values.put(Images.Media.MIME_TYPE, "image/jpeg");
        values.put(Media.DESCRIPTION, "Image capture by camera");

      photoUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
      
      Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      i.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

      Log.v("content uri: ", photoUri.toString());

      startActivityForResult(i, 10);
   }
   
   protected void onActivityResult(int requestCode, int resultCode, Intent data) // <-- data is null
   {
      if (requestCode == 10)
      {
         if (resultCode == RESULT_OK) {
            Log.v("onActivityResult", ":" + photoUri.toString());
            ImageView iv = (ImageView) this.findViewById(R.id.ImageView01);
            iv.setImageURI(photoUri);
         }
         else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "RESULT_CANCELED",
                  Toast.LENGTH_SHORT);
         }
      }
   }
cargo100
Freshman
Freshman
 
Posts: 2
Joined: Mon Mar 29, 2010 11:52 pm

Top

Re: cant even take a picture and show it in an imageview!

Postby Vishal R » Tue Dec 21, 2010 8:05 am

values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Media.DESCRIPTION, "Image capture by camera");
photoUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);

Instead of this try using

String url = Environment.getExternalStorageDirectory() + "Images.jpg";
Uri mImageCaptureUri = Uri.fromFile(new File (url));
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
Vishal R
Freshman
Freshman
 
Posts: 3
Joined: Tue Dec 21, 2010 7:22 am

Re: cant even take a picture and show it in an imageview!

Postby jr83 » Thu Feb 17, 2011 11:05 am

Not sure if you have solved this issue but here is a simple solution I used for my app:

When opting to capture an image from the camera I have the following code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  2.                         startActivityForResult(cameraIntent, 7);
  3.                        
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Then when the photo has been taken my onActivityResult looks like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  final ContentResolver cr = getContentResolver();    
  2.                            final String[] p1 = new String[] {
  3.                                            MediaStore.Images.ImageColumns._ID,
  4.                                            MediaStore.Images.ImageColumns.DATE_TAKEN
  5.                            };
  6.                            Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");      
  7.                            if ( c1.moveToFirst() ) {
  8.                                  
  9.                                   String uristringpic = "content://media/external/images/media/" +c1.getInt(0);
  10.                                   Uri newuri = Uri.parse(uristringpic);
  11.                                   ImageView icon= (ImageView) findViewById(R.id.actual_photo_four);
  12.                                   icon.setImageURI(newuri);
  13.                         }
  14.                            c1.close();
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I get the last image taken and display it.
Hope this helps!
jr83
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Sep 16, 2010 5:21 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 13 guests