Back to project page android_tutorial_projects.
The source code is released under:
Copyright (c) 2013, Uthcode All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redi...
If you think the Android project android_tutorial_projects listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.uthcode.imagepicker; //from w ww . j a va 2s .c om import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; import android.widget.ImageView; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class ImagePickActivity extends ActionBarActivity { private static final int REQUEST_CODE = 1; private Bitmap bitmap; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_pick); imageView = (ImageView) findViewById(R.id.result); } public void pickImage(View view) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(intent, REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { InputStream stream = null; if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) { try { if (bitmap != null) { bitmap.recycle(); } stream = getContentResolver().openInputStream(data.getData()); bitmap = BitmapFactory.decodeStream(stream); imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if ( stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }