标签:
##需求:个人界面的,个人头像图片的切换
*我在pop确定的点击方法中写了一个方法—–实现打开系统的相册并且获取到照片路径,在这里我们一开始就设置了请求码,用来区分onActivityResult,然后在本Activity中通过onActivityResult方法中通过请求码做相对应的处理
/**
* 从相册获取
*/
protected void toAlbum() {
try {
Intent intentFromGallery = new Intent();
intentFromGallery.setType("image/*"); // 设置文件类型
intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
LogUtil.e("第1步","第1步"+"第1步,开始从相册获取");
startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 结果码不等于取消时候
if (resultCode != RESULT_CANCELED) {
switch (requestCode) {
case REQ_RESULT_EDITE_SET:
String text=data.getStringExtra(EditSetActivity.KEY_OBJ_CONTENT);
et_person_nickname.setText(""+text);
break;
case IMAGE_REQUEST_CODE: //这个是打开相册的请求码
LogUtil.e("第二步","第二步"+"第二步,打开系统裁剪工具");
LogUtil.e("第二步","第二步"+data.getData());
//content://media/external/images/media/9423
resizeImage(data.getData()); //第三步调用这个方法进行裁剪
break;
case CAMERA_REQUEST_CODE:
if (FileUtil.isSDCardEnable()) {
File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME);
resizeImage(Uri.fromFile(file));
} else {
// showToast("未找到存储卡,无法存储照片!");
ToastUtil.showShort(PersonalInfoActivity.this, "未找到存储卡,无法存储照片!");
}
break;
case RESIZE_REQUEST_CODE:
if (data != null) {
showResizeImage(data);
LogUtil.e("第4步","第4步"+"第4步,裁剪完成");
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 裁剪图片
* @param uri
*/
public void resizeImage(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
LogUtil.e("第三步","第三步"+"第三步,开始裁剪");
//打开裁剪的activity,并且获取到裁剪图片(在第二步的RESIZE_REQUEST_CODE请求码中处理)
startActivityForResult(intent, RESIZE_REQUEST_CODE);
}
/**
* 获取到裁剪的图片,(并且设置到imageview)
* @param data
*/
private void showResizeImage(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
file = FileUtil.saveBitmapToFile(photo, Environment.getExternalStorageDirectory().toString(), IMAGE_FILE_NAME);
Drawable drawable = new BitmapDrawable(photo);
iv_person.setImageDrawable(drawable);
}
}
/**
* 拍照
*/
protected void toCamera() {
try {
//MediaStore.ACTION_IMAGE_CAPTURE 表示拍照到指定的目录
Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 判断存储卡是否可以用,可用进行存储
if (FileUtil.isSDCardEnable()) {
//参数(MediaStore.EXTRA_OUTPUT,uri)//图片保存为临时文件 == EXTRA_OUTPUT
intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "face.jpg")));
}
startActivityForResult(intentFromCapture, CAMERA_REQUEST_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
onActivityResult() //方法中
case CAMERA_REQUEST_CODE: //拍完照的
if (FileUtil.isSDCardEnable()) {
File file = new File(Environment.getExternalStorageDirectory(), "face.jpg");
resizeImage(Uri.fromFile(file));
} else {
// showToast("未找到存储卡,无法存储照片!");
ToastUtil.showShort(PersonalInfoActivity.this, "未找到存储卡,无法存储照片!");
}
break;
/**
* 裁剪图片
* @param uri
*/
public void resizeImage(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
LogUtil.e("第三步","第三步"+"第三步,开始裁剪");
//打开裁剪的activity,并且获取到裁剪图片(在第二步的RESIZE_REQUEST_CODE请求码中处理)
startActivityForResult(intent, RESIZE_REQUEST_CODE);
}
/**
* 获取到裁剪的图片,(并且设置到imageview)
* @param data
*/
private void showResizeImage(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
file = FileUtil.saveBitmapToFile(photo, Environment.getExternalStorageDirectory().toString(), IMAGE_FILE_NAME);
Drawable drawable = new BitmapDrawable(photo);
iv_person.setImageDrawable(drawable);
}
}
以上就是两种方式上传图像的方式
使用的utile贴在下面
FileUtil
package com.lzyc.ybtappcal.util;
import android.graphics.Bitmap;
import android.os.Environment;
import android.os.StatFs;
import com.lzyc.ybtappcal.app.App;
import com.lzyc.ybtappcal.exception.ServiceRulesException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
/*
* Author: lovelin
*
* Created Date:2015-4-7
* Copyright @ 2015 BU
* Description: 文件工具类
*
* History:
*/
public class FileUtil {
public static String appFile = App.AppFilePath;// 默认app文件目录
public static final String baseFile = Environment
.getExternalStorageDirectory() + File.separator;
public static final String appBaseFile = baseFile + appFile
+ File.separator;
boolean sdCardExist = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);// 是否有存储设备
public FileUtil() throws ServiceRulesException {
if (!sdCardExist)
throw new ServiceRulesException("请插入外部SD存储卡");
File fileBase = new File(appBaseFile);
if (!fileBase.exists())
fileBase.mkdir();
}
/**
* 创建一个文件目录
*
* @return void
* @author Lucifer 2015-4-8 下午8:02:31
*/
public static File creatFileDirectory(String path) {
File file = new File(appBaseFile + File.separator + path);
if (!file.exists()) {
file.mkdirs();
}
return file;
}
/**
* 创建一个文件
*
* @param path
* @param fileName
* @return File
* @author Lucifer 2015-4-8 下午8:28:54
*/
public static File creatNewFile(String path, String fileName) {
File file = null;
creatFileDirectory(path);
file = new File(appBaseFile + File.separator + path + File.separator
+ fileName);
return file;
}
/**
* 将图片写入当前文件中
*
* @param photo
* @param path
* @param fileName
* @return File
* @author Lucifer 2015-4-8 下午8:38:17
*/
public static File saveBitmapToFile(Bitmap photo, String path, String fileName) {
File file = null;
file = creatNewFile(path, fileName);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
photo.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 写到sd卡
* @param path
* @param fileName
* @param inputStream
* @return File
* @author luxf 2015-5-26 下午2:37:43
*/
public static File write2SDFormInput(String path, String fileName,
InputStream inputStream) {
// 创建文件
File file = creatNewFile(path, fileName);
OutputStream outputStream = null;
try {
// 创建输出流
outputStream = new FileOutputStream(file);
// 创建缓冲区
byte buffer[] = new byte[4 * 1024];
// 写入数据
while ((inputStream.read(buffer)) != -1) {
outputStream.write(buffer);
}
// 清空缓存
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
/**
* 获取文件大小
*
* @param file
* @return long
* @throws Exception
* @author Administrator 2015-6-14 下午8:22:02
*/
public static long getFileSize(File file) throws Exception {
long size = 0;
File flist[] = file.listFiles();
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getFileSize(flist[i]);
} else {
size = size + flist[i].length();
}
}
return size;
}
/**
* 转换文件 大小
*
* @param fileS
* @return String
* @author Administrator 2015-6-14 下午8:22:14
*/
public static String FormetFileSize(long fileS) {// 转换文件大小
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS == 0) {
fileSizeString = "0.0B";
} else if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}
/**
* 递归删除文件和文件夹
*
* @param file 要删除的根目录
*/
public static void RecursionDeleteFile(File file) {
if (file.isFile()) {
file.delete();
return;
}
if (file.isDirectory()) {
File[] childFile = file.listFiles();
if (childFile == null || childFile.length == 0) {
file.delete();
return;
}
for (File f : childFile) {
RecursionDeleteFile(f);
}
file.delete();
}
}
/**
* 判断文件是否存在
*
* @param path
* @param fileName
* @return boolean
* @author Administrator 2015-6-16 下午10:32:40
*/
public static boolean isFileExistes(String path, String fileName) {
try {
File f = new File(path + fileName);
if (!f.exists()) {
return false;
}
} catch (Exception e) {
return false;
}
return true;
}
/**
* 判断SDCard是否可用
*
* @return
*/
public static boolean isSDCardEnable() {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
/**
* 获取SD卡路径
*
* @return
*/
public static String getSDCardPath() {
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator;
}
/**
* 获取SD卡的剩余容量 单位byte
*
* @return
*/
public static long getSDCardAllSize() {
if (isSDCardEnable()) {
StatFs stat = new StatFs(getSDCardPath());
// 获取空闲的数据块的数量
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
// 获取单个数据块的大小(byte)
long freeBlocks = stat.getAvailableBlocks();
return freeBlocks * availableBlocks;
}
return 0;
}
/**
* 获取指定路径所在空间的剩余可用容量字节数,单位byte
*
* @param filePath
* @return 容量字节 SDCard可用空间,内部存储可用空间
*/
public static long getFreeBytes(String filePath) {
// 如果是sd卡的下的路径,则获取sd卡可用容量
if (filePath.startsWith(getSDCardPath())) {
filePath = getSDCardPath();
} else {// 如果是内部存储的路径,则获取内存存储的可用容量
filePath = Environment.getDataDirectory().getAbsolutePath();
}
StatFs stat = new StatFs(filePath);
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
return stat.getBlockSize() * availableBlocks;
}
}
个人界面 < 头像 > 图片选择(相册,拍照)--如何调用系统的相册,裁剪并且上传
标签:
原文地址:http://blog.csdn.net/tongzhengtong/article/details/51720640