Skip to content

Instantly share code, notes, and snippets.

@TheLittleNaruto
Created April 28, 2016 09:19
Show Gist options
  • Save TheLittleNaruto/ff0a264ad08c9f166de5585f4e65f27d to your computer and use it in GitHub Desktop.
Save TheLittleNaruto/ff0a264ad08c9f166de5585f4e65f27d to your computer and use it in GitHub Desktop.
Capture from gallery
private void chooseImageFromGallery() {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), USER_IMG_GALLERY_REQ);
}
private String getRealPathFromURI(Uri contentURI) {
String imgPath = "";
if (Build.VERSION.SDK_INT < 11)
imgPath = RealPathUtil.getRealPathFromURI_BelowAPI11(getActivity(), contentURI);
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19)
imgPath = RealPathUtil.getRealPathFromURI_API11to18(getActivity(), contentURI);
// SDK > 19 (Android 4.4)
else if (Build.VERSION.SDK_INT > 19) {
try {
imgPath = RealPathUtil.getRealPathFromURI_API19(getActivity(), contentURI);
} catch (Exception e) {
imgPath = getPath(contentURI);
}
} else
imgPath = getPath(contentURI);
return imgPath;
}
private String getPath(Uri contentURI) {
Cursor cursor = getActivity().getContentResolver().query(contentURI, null, null,
null, null);
if (cursor == null) // Source is Dropbox or other similar local file
// path
return contentURI.getPath();
else if (cursor.getCount() > 0) {
cursor.moveToFirst();
int idx = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
} else {
cursor.moveToFirst();
int idx = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}
private Bitmap getBitmap(String path) {
Uri uri = Uri.fromFile(new File(path));
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 100000; // 1.2MP
in = getActivity().getContentResolver()
.openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE) {
scale++;
}
Bitmap b = null;
in = getActivity().getContentResolver()
.openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;
Matrix matrix = new Matrix();
matrix.postRotate(ImageOrientation.getImageOrientation(path));
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
(int) y, true);
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(),
matrix, true);
b.recycle();
b = rotatedBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
return b;
} catch (IOException e) {
return null;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
View view = getActiveView(mViewPager);
ImageView imgView = (ImageView) view.findViewById(R.id.documentImg);
switch (requestCode) {
case USER_IMG_GALLERY_REQ:
String path = data.getData().getPath();
imgView.setImageBitmap(getBitmap(getRealPathFromURI(data.getData())));
break;
}
}
package utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
/**
* Created by Administrator on 12/14/2015.
*/
public class RealPathUtil {
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index
= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment