Skip to content

Instantly share code, notes, and snippets.

@neonankiti
Last active April 9, 2016 09:49
Show Gist options
  • Save neonankiti/cc08c6cf0bf5ffb0e9271f3521e30fee to your computer and use it in GitHub Desktop.
Save neonankiti/cc08c6cf0bf5ffb0e9271f3521e30fee to your computer and use it in GitHub Desktop.
How to pick images from document. (DocumentProvider)
public class MainActivity extends AppCompatActivity {
private static final int CODE_PICKER = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDocumentPicker();
}
private void showDocumentPicker() {
// this is for editing and deleting. the other is ACTION_GET_DOCUMENT
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// add category filtering with openable document in the device.
intent.addCategory(Intent.CATEGORY_OPENABLE);
// filtering with mime type
intent.setType("image/*");
startActivityForResult(intent, CODE_PICKER);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CODE_PICKER) {
switch (resultCode) {
case Activity.RESULT_OK:
try {
runInBackground(data);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case Activity.RESULT_CANCELED:
break;
}
}
}
private Bitmap bitmap;
private void runInBackground(@Nullable final Intent data) throws InterruptedException {
Thread th = new Thread(new Runnable() {
@Override
public void run() {
try {
bitmap = getPickerBitmap(data);
} catch (IOException e) {
e.printStackTrace();
}
Log.d("bitmap instance is ", "" + bitmap);
}
});
th.start();
th.join();
ImageView bitmapView = (ImageView) findViewById(R.id.bitmap);
bitmapView.setImageBitmap(bitmap);
}
@Nullable
private Bitmap getPickerBitmap(@Nullable Intent data) throws IOException {
if (data == null) {
return null;
}
Uri uri = data.getData();
if (uri == null) {
return null;
}
ParcelFileDescriptor parcelFileDescriptor
= getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return bitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment