To upload a picture from the gallery, you need to send a similar content to the system:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); Intent chooserIntent = Intent.createChooser(intent, "Выберите изображение"); startActivityForResult(chooserIntent, FILE_SELECT_CODE);
The system will return the Uri
selected file.
protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case FILE_SELECT_CODE: if (resultCode == RESULT_OK) { Uri uri = data.getData(); if (uri != null) { // TODO: вот выбранный файл } } } }
Uri
will look like "context: \ ....." You can get a data stream from it like this:
InputStream fileStream = null; try { fileStream = getContentResolver().openInputStream(uri); } catch (IOException e) { Log.e(this.getClass().getName(), e.getMessage()); }
How to work effectively with pictures and upload them in the desired resolution is well described here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
BitmapFactory
can work not only with application resources, but also with files and streams.