Skip to content Skip to sidebar Skip to footer

Android - Camera Activity Gets Opened Instead Of Picture Gallery

I've created an activity that allows the user to add a picture to an ImageView in that activity. I've created a button, that opens a context menu for the activity and gives the use

Solution 1:

i used to call camera activity from my activity, and it worked well, here is my code:

 setMediaUri(getNewMediaFilePath(actOwner.getContentResolver()));
                IntentcameraIntent=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getMediaUri());
                startActivityForResult(cameraIntent, CAMERA_CAPTURE_REQUEST_CODE);

and when result callback

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK 
            && requestCode == CAMERA_CAPTURE_REQUEST_CODE) {
        Drawable toRecycle= imgView.getDrawable();
        if (toRecycle != null) {                
            ((BitmapDrawable)imgView.getDrawable()).getBitmap().recycle();
        }
        mImg = decodeFileIntoRequiredSize(getPath(getMediaUri(),this), requiredSizeForImage);
        imgView.setImageBitmap(mImg);           
    } 
public Bitmap decodeFileIntoRequiredSize(String filePath,int requiredSize){
    Bitmapb=null;
    try {
        Filef=newFile(filePath);
        //Decode image size
        BitmapFactory.Optionso=newBitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStreamfis=newFileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();
        intscale=1;
        if (o.outHeight > requiredSize || o.outWidth > requiredSize) {
            scale = (int)Math.pow(2, (int) Math.round(Math.log(requiredSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        //Decode with inSampleSize
        BitmapFactory.Optionso2=newBitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = newFileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
    }
    return b;
}
public String getPath(Uri uri,Activity act) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursorcursor= act.managedQuery(uri, projection, null, null, null);
    if (cursor != null) {       
        intcolumn_index= cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } elsereturnnull;
}
public Uri getNewMediaFilePath(ContentResolver contentResolver) {
    ContentValuesvalues=newContentValues();

    //create the directory// /mnt/sdcard/DCIM/Camera/IMG_20111101_111922.jpgStringcameraDir="/Camera";
    //File dir1 = act.getExternalFilesDir(Environment.DIRECTORY_DCIM);Filedir=newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()+cameraDir);
    if(!dir.exists()) dir.mkdir();

    //use date as filenameStringname="IMG_" + (String) android.text.format.DateFormat.format("yyyyMMdd_hhmmss",newDate());
    Stringpath= dir.getPath()+File.separator + name;
    values.put(MediaStore.MediaColumns.TITLE, name);
    values.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis());
    Uribase=null;
    path += ".jpg";
    base = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.MediaColumns.DATA, path);
    return contentResolver.insert(base, values);
}

Hope this help you.

Post a Comment for "Android - Camera Activity Gets Opened Instead Of Picture Gallery"