0

I want to pick image from the gallery and use it in my App as Profile Picture. So far I've tried this.

Here's my InfoGet class. The loadImagefromGallery gets called on click.

 public void loadImagefromGallery(View view) {

  /*  Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(galleryIntent, RESULT_LOAD);*/
    Intent gallery_Intent = new Intent(getApplicationContext(), GalleryUtil.class);
    startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLERY_ACTIVITY_CODE) {
        if(resultCode == Activity.RESULT_OK){
         String picturePath = data.getStringExtra("picturePath");
            //perform Crop on the Image Selected from Gallery
            performCrop(picturePath);
        }
    }

    if (requestCode == RESULT_CROP ) {
        if(resultCode == Activity.RESULT_OK){
            Bundle extras = data.getExtras();
            Bitmap selectedBitmap = extras.getParcelable("data");
            // Set The Bitmap Data To ImageView
            pro.setImageBitmap(selectedBitmap);
            pro.setScaleType(ImageView.ScaleType.FIT_XY);
        }
    }
}

private void performCrop(String picUri) {
    try {
        //Start Crop Activity

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        // indicate image type and Uri
        File f = new File(picUri);
        Uri contentUri = Uri.fromFile(f);

        cropIntent.setDataAndType(contentUri, "image/*");
        // set crop properties
        cropIntent.putExtra("crop", "true");
        // indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        // indicate output X and Y
        cropIntent.putExtra("outputX", 280);
        cropIntent.putExtra("outputY", 280);

        // retrieve data on return
        cropIntent.putExtra("return-data", true);
        // start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, RESULT_CROP);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        // display an error message
        String errorMessage = "your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

And here's my GalleryUtil code

public class  GalleryUtil extends Activity {
private final static int RESULT_SELECT_IMAGE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final String TAG = "GalleryUtil";

String mCurrentPhotoPath;
File photoFile = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try{
        //Pick Image From Gallery
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_SELECT_IMAGE);
    }catch(Exception e){
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode){
        case RESULT_SELECT_IMAGE:

            if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
                try{
                    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();

                    //return Image Path to the Main Activity
                    Intent returnFromGalleryIntent = new Intent();
                    returnFromGalleryIntent.putExtra("picturePath",picturePath);
                    setResult(RESULT_OK,returnFromGalleryIntent);
                    finish();
                }catch(Exception e){
                    e.printStackTrace();
                    Intent returnFromGalleryIntent = new Intent();
                    setResult(RESULT_CANCELED, returnFromGalleryIntent);
                    finish();
                }
            }else{
                Log.i(TAG, "RESULT_CANCELED");
                Intent returnFromGalleryIntent = new Intent();
                setResult(RESULT_CANCELED, returnFromGalleryIntent);
                finish();
            }
            break;
    }
}

}

I need help can anyone just make some changes in the existing code. I do not want to rebuild anything from scratch. I want to optimise this image picker before I release the next update. The Existing code work okay for devices with decent amount of RAM like 1GB or so. Please help me. And also explain what you did and how it works. Thanks a LOT.

2
  • Likely duplicate question, check stackoverflow.com/questions/5697760/… and see if you have a unique problem or it has already been covered elsewhere on stackoverflow. Commented Oct 8, 2015 at 18:05
  • I m not able to implement in my code. Help me do that Commented Oct 9, 2015 at 1:29

1 Answer 1

1

I did that method read images from path and solve this issue, maybe it help you:

public static Bitmap readBitmap(Uri selectedImage, int resizeFactor)
    {
        Bitmap bm = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = resizeFactor;
        AssetFileDescriptor fileDescriptor = null;
        try
        {
            fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
                fileDescriptor.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return bm;
    }

The atributes are Uri selectedImage and int resizeFactor;

on selectedImage you need to put a Path from image and on resizeFactor put a int number, Normally I use number 10, try with that or try with another one (down/up)

Now if you want to put on ImageView you can use that.

ImageView.setImageBitmap(readBitmap(Path, 10));

Later you need clear Bitmap content to avoid OutOfMemory, use that method:

public static void clearBitmap(Bitmap bm)
    {
        bm.recycle();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Will this work?Where to call clearBitmap? public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == GALLERY_ACTIVITY_CODE) { if(resultCode == Activity.RESULT_OK){ String picturePath = data.getStringExtra("picturePath"); //perform Crop on the Image Selected from Gallery readBitmap(Uri.parse(picturePath), 10); performCrop(picturePath); } } & theres an error on context in front of fileDescriptor
fileDescriptor = Context.getContentResolver().openAssetFileDescriptor(selectedImage, "r"); getContentResolver() cannot be refrenced from static context.
okay, remove static from my method or change your class to final class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.