Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i have found the solution to get the image from gallery and uploading it. Some images selected from gallery may look rotated, in that case the following solution works good</p> <p>Select image from gallery</p> <pre><code>Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult(intent, 2); </code></pre> <p>Next in onActivityResult</p> <pre><code>public void onActivityResult(int requestCode, int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == Activity.RESULT_OK ) { f(requestCode == 2) { try { String [] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(data.getData(), proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); pathInput = cursor.getString(column_index); Appconstants.f = Environment.getExternalStorageDirectory() + "/tmp_siva.jpg"; ImageUtils.resampleImageAndSaveToNewLocation(pathInput, Appconstants.f); } catch (Exception ex) { Log.e("Exception ex @ try catch",""+ex); } } } } </code></pre> <p>Here is the ImageUtils class</p> <pre><code>public class ImageUtils { private ImageUtils() { } public static void resampleImageAndSaveToNewLocation(String pathInput, String pathOutput) throws Exception { Bitmap bmp = resampleImage(pathInput, 800); OutputStream out = new FileOutputStream(pathOutput); bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); } public static Bitmap resampleImage(String path, int maxDim) throws Exception { BitmapFactory.Options bfo = new BitmapFactory.Options(); bfo.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, bfo); BitmapFactory.Options optsDownSample = new BitmapFactory.Options(); optsDownSample.inSampleSize = getClosestResampleSize(bfo.outWidth, bfo.outHeight, maxDim); Bitmap bmpt = BitmapFactory.decodeFile(path, optsDownSample); Matrix m = new Matrix(); if (bmpt.getWidth() &gt; maxDim || bmpt.getHeight() &gt; maxDim) { BitmapFactory.Options optsScale = getResampling(bmpt.getWidth(), bmpt.getHeight(), maxDim); m.postScale((float)optsScale.outWidth / (float)bmpt.getWidth(), (float)optsScale.outHeight / (float)bmpt.getHeight()); } int sdk = new Integer(Build.VERSION.SDK).intValue(); if (sdk &gt; 4) { int rotation = ExifUtils.getExifRotation(path); if (rotation != 0) { m.postRotate(rotation); } } return Bitmap.createBitmap(bmpt, 0, 0, bmpt.getWidth(), bmpt.getHeight(), m, true); } private static BitmapFactory.Options getResampling(int cx, int cy, int max) { float scaleVal = 1.0f; BitmapFactory.Options bfo = new BitmapFactory.Options(); if (cx &gt; cy) { scaleVal = (float)max / (float)cx; } else if (cy &gt; cx) { scaleVal = (float)max / (float)cy; } else { scaleVal = (float)max / (float)cx; } bfo.outWidth = (int)(cx * scaleVal + 0.5f); bfo.outHeight = (int)(cy * scaleVal + 0.5f); return bfo; } private static int getClosestResampleSize(int cx, int cy, int maxDim) { /*Log.e("cx",""+cx); Log.e("cy",""+cy);*/ int max = Math.max(cx, cy); int resample = 1; for (resample = 1; resample &lt; Integer.MAX_VALUE; resample++) { if (resample * maxDim &gt; max) { resample--; break; } } if (resample &gt; 0) { return resample; } return 1; } public static BitmapFactory.Options getBitmapDims(String path) throws Exception { BitmapFactory.Options bfo = new BitmapFactory.Options(); bfo.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, bfo); return bfo; } } </code></pre> <p>Here is the Exif class</p> <pre><code>public class ExifUtils { private ExifUtils() { } public static int getExifRotation(String imgPath) { try { ExifInterface exif = new ExifInterface(imgPath); String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION); if (!TextUtils.isEmpty(rotationAmount)) { int rotationParam = Integer.parseInt(rotationAmount); switch (rotationParam) { case ExifInterface.ORIENTATION_NORMAL: return 0; case ExifInterface.ORIENTATION_ROTATE_90: return 90; case ExifInterface.ORIENTATION_ROTATE_180: return 180; case ExifInterface.ORIENTATION_ROTATE_270: return 270; default: return 0; } } else { return 0; } } catch (Exception ex) { return 0; } } } </code></pre> <p>The image gets selected in gallery is been checked whether of portrait or landscape type and been rotated and saved in a new path in sdcard. To avoid OOM issue its been resized.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload