开发者

Android image picking issue from gallery

I'm new to android and I'm developing an app which uses gallery and image picking from gallery, but I'm getting an error which I have tried to solve in different way,as

I'm using gallery located on SD card when I pick the horizontal image (width greater than height) it loads the image in image view perfectly fine but the problem is with vertical image (height greater than width) it show the image in image view rotated on the left side(or saying -90 degree from original position). I have printed the width and height of the both images (horizontal and vertical) its show width=2592 and height=1936 for both images,

I'm really struck here and haven't able to solve it for about 3 weeks please anyone help me.

Thanks in advance

here is my code:

for starting gallery intent

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);

after selecting Image:

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        try{ switch(requestCode) { case REQ_CODE_PICK_IMAGE: if(resultCode == RESULT_OK)
        {
            Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            filePath = cursor.getString(columnIndex);
            cursor.close();

            File img = new File(filePath); Bitmap yourSelectedImage =  BitmapFactory.decodeStream(new FileInputStream(img));
       开发者_C百科     int imgHeight = yourSelectedImage.getHeight(); int imgWidth = yourSelectedImage.getWidth();
            Toast.makeText(getApplicationContext(), "Original size: Width ="+imgWidth+" Height="+imgHeight, Toast.LENGTH_LONG).show();
            float myScalingFactor = MyScaleFactor(imgHeight,imgWidth);
            int destW = (int)(imgWidth*myScalingFactor);
            int destH = (int)(imgHeight*myScalingFactor);
            Toast.makeText(getApplicationContext(), "Scaled size: Width ="+destW+" Height="+destH + "Scale" +myScalingFactor, Toast.LENGTH_LONG).show();
            yourSelectedImage = Bitmap.createScaledBitmap(yourSelectedImage, destW, destH, true);
            picUpload.setImageBitmap(yourSelectedImage);
}


I wrote an article about this. It depends on a library I made but you can get most of the functionality using the code that's in the article itself. Here's a summary...

Proper way to launch the activity. Basically, if someone quits the app while in the picking from the gallery and then returns to it 2 hours later via the launcher icon. It's disorienting to bring them back to the Gallery since they won't understand or remember why they were there.

final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
activity.startActivityForResult(intent, requestCode);

Retrieve the result...

final InputStream is = context.getContentResolver().openInputStream(intent.getData());
final Bitmap imageData = BitmapFactory.decodeStream(is, null, options);
is.close();

This code wraps handles more cases than that DB lookup stuff can. It's also cleaner.

That'll get you most bang for the buck and might even be enough for production code. I go into more details like the perils of loading very large images and how to handle them using BitmapFactory.Options.inJustDecodeBounds.


When you load the bitmap, you have to take orientation of the photo into consideration. That being said, you have to get exif information from image file or via content resolver.

You can check the rotate image part as described in https://gist.github.com/Mariovc/f06e70ebe8ca52fbbbe2 or check out my library droid-image-picker for selecting/croping images.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜