Android中调用系统的相机和图库获取图片

//--------我的主布局文件------很简单---------------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="gallery"
android:text="获取图库图片" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="camera"
android:text="拍照获取图片" />

<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


//------------------我的MainActivity --------------也很简单--------------------------

package tackpicture.bwie.com.tackpicture;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {

private ImageView iv_image;

private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
/* 头像名称 */
private static final String PHOTO_FILE_NAME = "temp_photo.jpg";
private File tempFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
iv_image = (ImageView) findViewById(R.id.iv_image);
}


//图库
public void camera(View view) {
// 激活系统图库,选择一张图片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}

//相机
public void gallery(View view) {
// 激活相机
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// 判断存储卡是否可以用,可用进行存储
if (hasSdcard()) {
tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME);
// 从文件中创建uri
Uri uri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA
startActivityForResult(intent, PHOTO_REQUEST_CAREMA);
}


/*
* 剪切图片
*/
private void crop(Uri uri) {
// 裁剪图片意图
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后输出图片的尺寸大小
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);

intent.putExtra("outputFormat", "JPEG");// 图片格式
intent.putExtra("noFaceDetection", true);// 取消人脸识别
intent.putExtra("return-data", true);
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}

/*
* 判断sdcard是否被挂载
*/
private boolean hasSdcard() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_REQUEST_GALLERY) {
// 从相册返回的数据
if (data != null) {
// 得到图片的全路径
Uri uri = data.getData();
crop(uri);
}
} else if (requestCode == PHOTO_REQUEST_CAREMA) {
// 从相机返回的数据
if (hasSdcard()) {
crop(Uri.fromFile(tempFile));
} else {
Toast.makeText(MainActivity.this, "未找到存储卡,无法存储照片!", 0).show();
}

} else if (requestCode == PHOTO_REQUEST_CUT) {
// 从剪切图片返回的数据
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
this.iv_image.setImageBitmap(bitmap);
}
try {
// 将临时文件删除
tempFile.delete();
} catch (Exception e) {
e.printStackTrace();
}

}

super.onActivityResult(requestCode, resultCode, data);
}


}



//-----------------以上就完了-----------------------------


下面看一下怎么把图片的Bitmap保存到SharedPreferences

SharedPreferences详解(三)——存取图片

  (2014-11-27 17:34:43)

标签: 

android

分类: 软件开发

这篇博文来自 http://blog.csdn.net/lfdfhl/article/details/37914673;非常感谢作者的分享
 
 
MainActivity如下:
  1. package cc.sp;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import android.os.Bundle;  
  6. import android.util.Base64;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11. import android.app.Activity;  
  12. import android.content.Context;  
  13. import android.content.SharedPreferences;  
  14. import android.content.SharedPreferences.Editor;  
  15. import android.graphics.Bitmap;  
  16. import android.graphics.Bitmap.CompressFormat;  
  17. import android.graphics.BitmapFactory;  
  18.   
  19. public class MainActivity extends Activity {  
  20.     private Button mSaveButton;  
  21.     private Button mGetButton;  
  22.     private ImageView mImageView;  
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.         init();  
  28.     }  
  29.       
  30.     private void init(){  
  31.         mSaveButton=(Button) findViewById(R.id.saveButton);  
  32.         mSaveButton.setOnClickListener(new OnClickListener() {  
  33.             @Override  
  34.             public void onClick(View view) {  
  35.                 saveBitmapToSharedPreferences();  
  36.             }  
  37.         });  
  38.         mGetButton=(Button) findViewById(R.id.getButton);  
  39.         mGetButton.setOnClickListener(new OnClickListener() {  
  40.             @Override  
  41.             public void onClick(View view) {  
  42.                 getBitmapFromSharedPreferences();  
  43.             }  
  44.         });  
  45.         mImageView=(ImageView) findViewById(R.id.imageView);  
  46.     }  
  47.   
  48.       
  49.       
  50.     private void saveBitmapToSharedPreferences(){  
  51.         Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);  
  52.         //第一步:将Bitmap压缩至字节数组输出流ByteArrayOutputStream  
  53.         ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();  
  54.         bitmap.compress(CompressFormat.PNG, 80, byteArrayOutputStream);  
  55.         //第二步:利用Base64将字节数组输出流中的数据转换成字符串String  
  56.         byte[] byteArray=byteArrayOutputStream.toByteArray();  
  57.         String imageString=new String(Base64.encodeToString(byteArray, Base64.DEFAULT));  
  58.         //第三步:将String保持至SharedPreferences  
  59.         SharedPreferences sharedPreferences=getSharedPreferences("testSP", Context.MODE_PRIVATE);  
  60.         Editor editor=sharedPreferences.edit();  
  61.         editor.putString("image", imageString);  
  62.         editor.commit();  
  63.     }  
  64.       
  65.       
  66.       
  67.     private void getBitmapFromSharedPreferences(){  
  68.         SharedPreferences sharedPreferences=getSharedPreferences("testSP", Context.MODE_PRIVATE);  
  69.         //第一步:取出字符串形式的Bitmap  
  70.         String imageString=sharedPreferences.getString("image", "");  
  71.         //第二步:利用Base64将字符串转换为ByteArrayInputStream  
  72.         byte[] byteArray=Base64.decode(imageString, Base64.DEFAULT);  
  73.         ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);  
  74.         //第三步:利用ByteArrayInputStream生成Bitmap  
  75.         Bitmap bitmap=BitmapFactory.decodeStream(byteArrayInputStream);  
  76.         mImageView.setImageBitmap(bitmap);  
  77.     }  
  78.       

//-------------------------------布局文件------------------------------------

main.xml如下:
    1. <</span>RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:tools="http://schemas.android.com/tools"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.    >  
    6.   
    7.     <</span>Button  
    8.         android:id="@+id/saveButton"  
    9.         android:layout_width="wrap_content"  
    10.         android:layout_height="wrap_content"  
    11.         android:text="保存图片到SharedPreferences"   
    12.         android:layout_centerHorizontal="true"  
    13.         android:layout_marginTop="25dip"/>  
    14.       
    15.      <</span>Button  
    16.         android:id="@+id/getButton"  
    17.         android:layout_width="wrap_content"  
    18.         android:layout_height="wrap_content"  
    19.         android:text="从SharedPreferences获取图片"   
    20.         android:layout_centerHorizontal="true"  
    21.         android:layout_marginTop="80dip"/>  
    22.        
    23.        
    24.      <</span>ImageView   
    25.         android:id="@+id/imageView"  
    26.         android:layout_width="wrap_content"  
    27.         android:layout_height="wrap_content"  
    28.         android:layout_centerInParent="true"  
    29.         />  
    30.   
    31. </</span>RelativeLayout> 


原文地址:https://www.cnblogs.com/changyiqiang/p/6144329.html