Open Images Amongst Intent.Action_Open_Document, Intent.Action_Get_Content In Addition To Intent.Action_Pick

Posted on

Example exhibit how to pick out images past times calling startActivityForResult() with Intent.ACTION_OPEN_DOCUMENT (Android 4.4, API score 19, in addition to higher), Intent.ACTION_GET_CONTENT in addition to Intent.ACTION_PICK, in addition to then charge it using InputStream.

Tested on Nexus vii running Android 5.1.1, to charge photos from local in addition to from cloud of Google Photos.

In my test:
– if pick out photos with Intent.ACTION_OPEN_DOCUMENT or Intent.ACTION_GET_CONTENT, both photos on local storage in addition to on Google Photos cloud storage tin hold upwards loaded using InputStream.
– if pick out photos with Intent.ACTION_PICK, cannot charge from Google Photos cloud storage. (or may hold upwards past times other method, I don’t know).


MainActivity.java

package com.blogspot.android_er.androidloadphoto;  import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;  import java.io.FileNotFoundException; import java.io.InputStream;  populace class MainActivity extends AppCompatActivity {      Button btnOpen, btnGet, btnPick;     TextView textInfo1, textInfo2;     ImageView imageView;      someone static concluding int RQS_OPEN_IMAGE = 1;     someone static concluding int RQS_GET_IMAGE = 2;     someone static concluding int RQS_PICK_IMAGE = 3;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btnOpen = (Button)findViewById(R.id.open);         btnGet = (Button)findViewById(R.id.get);         btnPick = (Button)findViewById(R.id.pick);         textInfo1 = (TextView)findViewById(R.id.info1);         textInfo2 = (TextView)findViewById(R.id.info2);         imageView = (ImageView) findViewById(R.id.image);          btnOpen.setOnClickListener(btnOpenOnClickListener);         btnGet.setOnClickListener(btnGetOnClickListener);         btnPick.setOnClickListener(btnPickOnClickListener);     }      View.OnClickListener btnOpenOnClickListener = novel View.OnClickListener() {         @Override         populace void onClick(View v) {             Intent intent = novel Intent();             intent.setAction(Intent.ACTION_OPEN_DOCUMENT);             intent.addCategory(Intent.CATEGORY_OPENABLE);             intent.setType("image/*");              startActivityForResult(intent, RQS_OPEN_IMAGE);         }     };      View.OnClickListener btnGetOnClickListener = novel View.OnClickListener() {         @Override         populace void onClick(View v) {             Intent intent = novel Intent();             intent.setAction(Intent.ACTION_GET_CONTENT);             intent.addCategory(Intent.CATEGORY_OPENABLE);             intent.setType("image/*");              startActivityForResult(intent, RQS_OPEN_IMAGE);         }     };      View.OnClickListener btnPickOnClickListener = novel View.OnClickListener() {         @Override         populace void onClick(View v) {             Intent intent = novel Intent(Intent.ACTION_PICK,                     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);             startActivityForResult(intent, RQS_PICK_IMAGE);         }     };      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if (resultCode == Activity.RESULT_OK) {               if (requestCode == RQS_OPEN_IMAGE ||                     requestCode == RQS_GET_IMAGE ||                     requestCode == RQS_PICK_IMAGE) {                  imageView.setImageBitmap(null);                 textInfo1.setText("");                 textInfo2.setText("");                  Uri mediaUri = data.getData();                 textInfo1.setText(mediaUri.toString());                 String mediaPath = mediaUri.getPath();                 textInfo2.setText(mediaPath);                  //display the icon                 endeavor {                     InputStream inputStream = getBaseContext().getContentResolver().openInputStream(mediaUri);                     Bitmap bm = BitmapFactory.decodeStream(inputStream);                     imageView.setImageBitmap(bm);                 } grab (FileNotFoundException e) {                     e.printStackTrace();                 }             }         }     } }  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidloadphoto.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />     <Button         android:id="@+id/open"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="OPEN Image"/>     <Button         android:id="@+id/get"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="GET Image"/>     <Button         android:id="@+id/pick"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="PICK Image"/>     <TextView         android:id="@+id/info1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="bold"/>     <TextView         android:id="@+id/info2"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="italic"/>     <ImageView         android:id="@+id/image"         android:layout_width="match_parent"         android:layout_height="match_parent" /> </LinearLayout>