Open Gallery for Android
One demo is used today, so I will share it with you and hope you will gain some benefits.
First:
Sample source code:
1. Add SD card Access Permissions
2. Source Code
package com.zengtao.demo;import java.io.FileNotFoundException;import android.content.ContentResolver;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends ActionBarActivity {private Button bt;private ImageView iv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt = (Button) findViewById(R.id.bt);iv = (ImageView) findViewById(R.id.iv);bt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction(Intent.ACTION_GET_CONTENT);intent.setType("image/*");startActivityForResult(intent, 1);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == 1) {if (resultCode == RESULT_OK) {if (data != null) {Uri uri = data.getData();ContentResolver cr = this.getContentResolver();try {Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));iv.setImageBitmap(bitmap);} catch (FileNotFoundException e) {e.printStackTrace();}}}}super.onActivityResult(requestCode, resultCode, data);}}
The above shows how to open the image library and return the selected image to the current application. Of course, there is still a defect in this process, that is, when the photo is too large, you need to correct it, the image obtained in the image library needs to be processed, that is, scaled down, so that no exception will occur. I believe other experts will mention this.