naresh
naresh

Reputation: 10392

android - How to get the images from sd card using camera

I implemented one application in that application one button is there. if you click on that button then camera will be opened using that u can capture some thing. That captured image will be displayed in the app.

But now i want to add one more button. if you click on that then it displays all images in the sdcard(camera related images) in grid view.if you select any image then it will be displayed in the app.

Please can any one suggest me how to implement it.

And one more thing. In this code

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  

Here "data" means what?

Code:

public class camera extends Activity 
    {

Button camera_btn;
ImageView cap_image;
public static final int CAMERA_PIC_REQUEST = 1;  

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    camera_btn = (Button) findViewById(R.id.camera_btn);
    camera_btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            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)
    {  
        if(data != null)
        {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  
            cap_image = (ImageView) findViewById(R.id.cap_image);
            cap_image.setImageBitmap(thumbnail); 
        }
        else
        {
            System.out.println("Please capature the photo");
        }

    }  
}  
}

Exception: 10-20 13:06:35.073: WARN/System.err(5391): java.lang.IllegalArgumentException: no dialog with id 10020 was ever shown via Activity#showDialog

10-20 13:06:35.073: WARN/System.err(5391): at android.app.Activity.missingDialog(Activity.java:2747)

10-20 13:06:35.073: WARN/System.err(5391): at android.app.Activity.dismissDialog(Activity.java:2732)

10-20 13:06:35.083: WARN/System.err(5391): at com.htc.album.TabPluginDevice.ActivityGlanceBase.handleActivityMessage(ActivityGlanceBase.java:161)

10-20 13:06:35.083: WARN/System.err(5391): at com.htc.opensense.album.TemplateActivityBase$ActivityHandler.handleMessage(TemplateActivityBase.java:89) 10-20 13:06:35.083: WARN/System.err(5391): at android.os.Handler.dispatchMessage(Handler.java:99)

10-20 13:06:35.083: WARN/System.err(5391): at android.os.Looper.loop(Looper.java:143)

10-20 13:06:35.083: WARN/System.err(5391): at android.app.ActivityThread.main(ActivityThread.java:4277)

10-20 13:06:35.093: WARN/System.err(5391): at java.lang.reflect.Method.invokeNative(Native Method)

10-20 13:06:35.093: WARN/System.err(5391): at java.lang.reflect.Method.invoke(Method.java:507)

10-20 13:06:35.093: WARN/System.err(5391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

10-20 13:06:35.093: WARN/System.err(5391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

10-20 13:06:35.093: WARN/System.err(5391): at dalvik.system.NativeStart.main(Native Method)

10-20 13:06:38.523: INFO/LogFilter(726): Couldn't find the mandatory "Host" HTTP header.

Upvotes: 1

Views: 1010

Answers (2)

naresh
naresh

Reputation: 10392

@Frankenstein: Entire process is right except the following code

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

Instead of this, do the following process

  1. Get the URI from the intent.

    Uri selectedImageUri = data.getData();

  2. Get the path from the URI

    String filestring = selectedImageUri.getPath();

  3. Get the bitmap using the getThumbnail (ContentResolver cr, long origId, int kind, BitmapFactory.Options options) method.

Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail( getContentResolver(), selectedImageUriId, MediaStore.Images.Thumbnails.MINI_KIND, (BitmapFactory.Options) null);

Here getContentResolver()---->Return a ContentResolver instance for your application's package. selectedImageUriId---->ID of the selected Thumbnail. This will be getting from the path.

Ex: Path: /external/images/media/5 Here "5" is the ID of the selected image. This "5" is getting from the path using string operations.

Finally the following code instead of the

Code

//Get the URI from the Intent

Uri selectedImageUri = data.getData();

//Get the decoded path from the URI

String filestring = selectedImageUri.getPath();

//Get the ID from the path. String x = filestring.substring(23);

long selectedImageUriId = Long.parseLong(x);

//Get the bitmap

Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail( getContentResolver(), selectedImageUriId, MediaStore.Images.Thumbnails.MINI_KIND, (BitmapFactory.Options) null);

Upvotes: 0

MKJParekh
MKJParekh

Reputation: 34291

    camera_btn.setOnClickListener(new OnClickListener(){ 

        @Override 
        public void onClick(View v) { 

            Intent cameraIntent = new Intent(Intent.ACTION_GET_CONTENT);
            cameraIntent.setType("image/*");
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
        } 

    }); 

and

protected void onActivityResult(int requestCode, int resultCode, Intent data)  
{   
    if (requestCode == CAMERA_PIC_REQUEST && resultCode==Activity.RESULT_OK) 
    {   
        if(data != null) 
        { 
        Uri contentUri = data.getData();
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String tmppath = cursor.getString(column_index);
        Bitmap bmp = BitmapFactory.decodeFile(tmppath);            } 
        else 
        { 
            System.out.println("Please capature the photo"); 
        } 

    }   
}   

and

Here "data" means what?

Dont have much conceptual knowledge, but you can consider data as the output of the activity that you have started for result..that is image in your case..also can be video or anything else depends upon what activity you started with which action.

Upvotes: 1

Related Questions