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
- package cc.sp;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import android.os.Bundle;
- import android.util.Base64;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.app.Activity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.CompressFormat;
- import android.graphics.BitmapFactory;
- public class MainActivity extends Activity {
- private Button mSaveButton;
- private Button mGetButton;
- private ImageView mImageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init(){
- mSaveButton=(Button) findViewById(R.id.saveButton);
- mSaveButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- saveBitmapToSharedPreferences();
- }
- });
- mGetButton=(Button) findViewById(R.id.getButton);
- mGetButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- getBitmapFromSharedPreferences();
- }
- });
- mImageView=(ImageView) findViewById(R.id.imageView);
- }
- private void saveBitmapToSharedPreferences(){
- Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
- //第一步:将Bitmap压缩至字节数组输出流ByteArrayOutputStream
- ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
- bitmap.compress(CompressFormat.PNG, 80, byteArrayOutputStream);
- //第二步:利用Base64将字节数组输出流中的数据转换成字符串String
- byte[] byteArray=byteArrayOutputStream.toByteArray();
- String imageString=new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
- //第三步:将String保持至SharedPreferences
- SharedPreferences sharedPreferences=getSharedPreferences("testSP", Context.MODE_PRIVATE);
- Editor editor=sharedPreferences.edit();
- editor.putString("image", imageString);
- editor.commit();
- }
- private void getBitmapFromSharedPreferences(){
- SharedPreferences sharedPreferences=getSharedPreferences("testSP", Context.MODE_PRIVATE);
- //第一步:取出字符串形式的Bitmap
- String imageString=sharedPreferences.getString("image", "");
- //第二步:利用Base64将字符串转换为ByteArrayInputStream
- byte[] byteArray=Base64.decode(imageString, Base64.DEFAULT);
- ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
- //第三步:利用ByteArrayInputStream生成Bitmap
- Bitmap bitmap=BitmapFactory.decodeStream(byteArrayInputStream);
- mImageView.setImageBitmap(bitmap);
- }
- }
//-------------------------------布局文件------------------------------------ main.xml如下:
- <</span>RelativeLayout 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"
- >
- <</span>Button
- android:id="@+id/saveButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="保存图片到SharedPreferences"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="25dip"/>
- <</span>Button
- android:id="@+id/getButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="从SharedPreferences获取图片"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="80dip"/>
- <</span>ImageView
- android:id="@+id/imageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- />
- </</span>RelativeLayout>
Android中调用系统的相机和图库获取图片的更多相关文章
- Java乔晓松-android中调用系统拍照功能并显示拍照的图片
android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...
- Android中调用系统所装的软件打开文件(转)
Android中调用系统所装的软件打开文件(转) 在应用中如何调用系统所装的软件打开一个文件,这是我们经常碰到的问题,下面是我所用到的一种方法,和大家一起分享一下! 这个是打开文件的一个方法: /** ...
- android中调用系统的发送短信、发送邮件、打电话功能
1 调用发送短信功能: Uri smsToUri = Uri.parse("smsto:"); Intent sendIntent = new Intent(Intent.ACT ...
- 关于android中调用系统拍照,返回图片是旋转90度
转载博客:http://blog.csdn.net/walker02/article/details/8211628 项目开发中遇到的一个问题,对于三星手机在做手机照片选择时出现图片显示不正常,研究后 ...
- 【转】Android 学习笔记——利用JNI技术在Android中调用、调试C++代码
原文网址:http://cherishlc.iteye.com/blog/1756762 在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在And ...
- Android中消息系统模型和Handler Looper
http://www.cnblogs.com/bastard/archive/2012/06/08/2541944.html Android中消息系统模型和Handler Looper 作为Andro ...
- [转][android][利用JNI技术在Android中调用、调试C++代码]
在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在Android中会生成Linux系统下的.so文件(好吧,其实我基本没用过Linux). 没写过 ...
- 在Android中调用WebService
某些情况下我们可能需要与Mysql或者Oracle数据库进行数据交互,有些朋友的第一反应就是直接在Android中加载驱动然后进行数据的增删改查.我个人不推荐这种做法,一是手机毕竟不是电脑,操作大量数 ...
- 在Android中调用C#写的WebService(附源代码)
由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebServi ...
随机推荐
- NYOJ-1057 寻找最大数(三)(贪心)
寻找最大数(三) 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给出一个整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的整数. 求这个新的整数的 ...
- Oracle SQL自带函数整理
数字函数 abs(n):用于返回数字n的绝对值 ceil(n):返回大于等于数字n的最小整数 floor(n):返回小于等于数字n的最大整数 mod(m,n):返回m/n数字相除后的余数,如果n=0, ...
- python 学习 有序字典
自定义创建有序字典类 dict的__getitem__方法 有些不同,想使用自定义__getitem__方法显示查询key的下标:需要研究 #/usr/bin/env python3 # -*- co ...
- javascript语言学习笔记。
js类创建方法 var DogKing = function(dogName){ this.dogName = dogName; }; var myDogKing = new DogKing(&quo ...
- centos7,yum安装的redis用systemctl无法启动
因为之前使用显示命令启动redis的,要使redis在后台运行就需要改redis.conf中的daemonize 为yes. 这次在centos7上也顺手改了为yes,然后使用systemctl启动, ...
- EventBus消息机制在Eclipse环境下的使用
1.在onStart()方法中注册 @Override public void onStart() { super.onStart(); // 注册 EventBus // 判断 Eventbus 是 ...
- 换行符在ajax中返回json,eval时发生的 Unexpected token ILLEGAL
用户如果输入了换行在数据中记录为‘空格’,但不是真正的空格. 程序前台采用ajax和json返回数据绑定时会 出现 Unexpected token ILLEGAL 例子: 在sql中存储为下图 在“ ...
- mui 访问手机自带是否连接网络
//mui检测是否连接网络 function getSysInfo() { // var str = ""; // str += "名称:" + plus. ...
- MongoDB高级查询用法大全
转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册: http://www.mongodb.org/d ...
- hdu1026
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struc ...