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

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

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. <</span>Button
    7. android:id="@+id/saveButton"
    8. android:layout_width="wrap_content"
    9. android:layout_height="wrap_content"
    10. android:text="保存图片到SharedPreferences"
    11. android:layout_centerHorizontal="true"
    12. android:layout_marginTop="25dip"/>
    13. <</span>Button
    14. android:id="@+id/getButton"
    15. android:layout_width="wrap_content"
    16. android:layout_height="wrap_content"
    17. android:text="从SharedPreferences获取图片"
    18. android:layout_centerHorizontal="true"
    19. android:layout_marginTop="80dip"/>
    20. <</span>ImageView
    21. android:id="@+id/imageView"
    22. android:layout_width="wrap_content"
    23. android:layout_height="wrap_content"
    24. android:layout_centerInParent="true"
    25. />
    26. </</span>RelativeLayout>
												

Android中调用系统的相机和图库获取图片的更多相关文章

  1. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

  2. Android中调用系统所装的软件打开文件(转)

    Android中调用系统所装的软件打开文件(转) 在应用中如何调用系统所装的软件打开一个文件,这是我们经常碰到的问题,下面是我所用到的一种方法,和大家一起分享一下! 这个是打开文件的一个方法: /** ...

  3. android中调用系统的发送短信、发送邮件、打电话功能

    1 调用发送短信功能: Uri smsToUri = Uri.parse("smsto:");  Intent sendIntent = new Intent(Intent.ACT ...

  4. 关于android中调用系统拍照,返回图片是旋转90度

    转载博客:http://blog.csdn.net/walker02/article/details/8211628 项目开发中遇到的一个问题,对于三星手机在做手机照片选择时出现图片显示不正常,研究后 ...

  5. 【转】Android 学习笔记——利用JNI技术在Android中调用、调试C++代码

    原文网址:http://cherishlc.iteye.com/blog/1756762 在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在And ...

  6. Android中消息系统模型和Handler Looper

    http://www.cnblogs.com/bastard/archive/2012/06/08/2541944.html Android中消息系统模型和Handler Looper 作为Andro ...

  7. [转][android][利用JNI技术在Android中调用、调试C++代码]

    在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在Android中会生成Linux系统下的.so文件(好吧,其实我基本没用过Linux). 没写过 ...

  8. 在Android中调用WebService

    某些情况下我们可能需要与Mysql或者Oracle数据库进行数据交互,有些朋友的第一反应就是直接在Android中加载驱动然后进行数据的增删改查.我个人不推荐这种做法,一是手机毕竟不是电脑,操作大量数 ...

  9. 在Android中调用C#写的WebService(附源代码)

    由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebServi ...

随机推荐

  1. NYOJ-1057 寻找最大数(三)(贪心)

    寻找最大数(三) 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 给出一个整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的整数. 求这个新的整数的 ...

  2. Oracle SQL自带函数整理

    数字函数 abs(n):用于返回数字n的绝对值 ceil(n):返回大于等于数字n的最小整数 floor(n):返回小于等于数字n的最大整数 mod(m,n):返回m/n数字相除后的余数,如果n=0, ...

  3. python 学习 有序字典

    自定义创建有序字典类 dict的__getitem__方法 有些不同,想使用自定义__getitem__方法显示查询key的下标:需要研究 #/usr/bin/env python3 # -*- co ...

  4. javascript语言学习笔记。

    js类创建方法 var DogKing = function(dogName){ this.dogName = dogName; }; var myDogKing = new DogKing(&quo ...

  5. centos7,yum安装的redis用systemctl无法启动

    因为之前使用显示命令启动redis的,要使redis在后台运行就需要改redis.conf中的daemonize 为yes. 这次在centos7上也顺手改了为yes,然后使用systemctl启动, ...

  6. EventBus消息机制在Eclipse环境下的使用

    1.在onStart()方法中注册 @Override public void onStart() { super.onStart(); // 注册 EventBus // 判断 Eventbus 是 ...

  7. 换行符在ajax中返回json,eval时发生的 Unexpected token ILLEGAL

    用户如果输入了换行在数据中记录为‘空格’,但不是真正的空格. 程序前台采用ajax和json返回数据绑定时会 出现 Unexpected token ILLEGAL 例子: 在sql中存储为下图 在“ ...

  8. mui 访问手机自带是否连接网络

    //mui检测是否连接网络 function getSysInfo() { //  var str = ""; //  str += "名称:" + plus. ...

  9. MongoDB高级查询用法大全

    转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册: http://www.mongodb.org/d ...

  10. hdu1026

    #include <stdio.h> #include <string.h> #include <queue> using namespace std; struc ...