保存照片和视频到系统相册显示- http://blog.csdn.net/chendong_/article/details/52290329

Android 7.0 之拍照与图片裁剪适配-http://blog.csdn.net/yyh352091626/article/details/54908624

拍照、相册及裁剪的终极实现(一)——拍照及裁剪功能实现- http://blog.csdn.net/harvic880925/article/details/43163175

看似简单的问题,在各个厂商手机上有不同的问题,请广而测之,

> 方案如下:将中间数据先暂存一下,然后再调裁剪Intent,最后把结果存在Uri中。
//打开相册
public void openAlbum() {
        Intent intent;
        if (Build.VERSION.SDK_INT < 19) {
            intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
        } else {
            intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        }
        startActivityForResult(intent, PHOTO_REQUEST_ALBUM);
    }

private File mPhotoFile;
//打开相机
    private void openCamera(File out) {
        LogUtil.e("openCamera", "mPhotoFile=" + mPhotoFile.toString());
// Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        try {
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out)); // set
// intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
            startActivityForResult(intent, PHOTO_REQUEST_CAREMA);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showLongToast(mContext,"当前无拍照权限,请在 设置-> 应用权限-> "+ mContext.getString(R.string.app_name)+"-> 权限-> 打开相机权限","");
        } finally {
        }
    }

private int PHOTO_REQUEST_CAREMA = 1001;// 拍照
private int PHOTO_REQUEST_ALBUM = 1002;// 从相册中选择
private int PHOTO_REQUEST_CUT = 1003;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 super.onActivityResult(requestCode, resultCode, data);
LogUtils.e("onActivityResult","exe onActivityResult()");
String picPath = null;
if (resultCode == RESULT_OK) {
if (requestCode == PHOTO_REQUEST_ALBUM) {// 相册
Uri selectedImage = data.getData();
if (data != null) {
Uri uri = data.getData();
crop(uri);
filePath = getRealPathFromURI(uri);
LogUtils.e("onActivityResult","filePath="+filePath);
}
LogUtil.e("onActivityResult", "相册 picPath= empty");
} else if (requestCode == PHOTO_REQUEST_CAREMA) {// 拍照
if (mPhotoFile != null && mPhotoFile.exists()) {
crop(Uri.fromFile(mPhotoFile));
filePath = mPhotoFile.toString();
LogUtils.e("onActivityResult","filePath="+filePath);
}
} else if (requestCode == PHOTO_REQUEST_CUT) {
Uri uri = data.getData();
LogUtils.e("PHOTO_REQUEST_CUT", "uri=" + uri);
if (uri != null) {
if (uri.toString().contains("content://")) { //如果包含有content开头,需要转化为其实际路径,不能用content开头
filePath = getRealPathFromURI(uri);
} else {
filePath = uri.toString(); //如果用file开头,不用转化
}
LogUtils.e("PHOTO_REQUEST_CUT", "filePath=" + filePath);
uploadAvatar();
} else {
uploadAvatar();
// ToastUtil.showShortToast(NyApplication.getInstance(), R.string.dataError);
}
} }

}

//裁剪图

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(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra("noFaceDetection", true);// 取消人脸识别
        intent.putExtra("return-data", false);// true:不返回uri,false:返回uri
        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

/**
     * 传入Uri,得到图片的真实路径
     *
     * @param contentUri
     * @return
     */
    private String getRealPathFromURI(Uri contentUri) { //传入图片uri地址
        String picPath = null;
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = null;
        cursor = mContext.getContentResolver().query(contentUri,
                filePathColumn, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor
                    .getColumnIndex(filePathColumn[0]);
            picPath = cursor.getString(columnIndex);
            cursor.close();
        }
        return picPath;
    }

Htc提示SD卡已满 Android -- https://zhidao.baidu.com/question/1048799384918824739.html

https://zhidao.baidu.com/question/2202745883929714388.html

public String getRealFilePath( final Context context, final Uri uri ) {
if ( null == uri ) return null;
final String scheme = uri.getScheme();
String data = null;
if ( scheme == null )
data = uri.getPath();
else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
data = uri.getPath();
} else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
if ( null != cursor ) {
if ( cursor.moveToFirst() ) {
int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
if ( index > -1 ) {
data = cursor.getString( index );
}
}
cursor.close();
}
}
return data;
}
/**
* 根据图片FileUrl路径获取ContentUri
*
* @param
* @param
* @return
*/
public Uri convertFileToUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null); if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}

图片路径URL与URI的相互转化???

>>> Android7.0 拍照及裁剪的坑

Android7.0适配心得-- http://www.tuicool.com/articles/zYniuyZ   ,   http://stackoverflow.com/questions/7305504/convert-file-uri-to-content-uri

> error:No such file or directory,java.io.UnixFileSystem.createFileExclusively0,文件夹目录需要先创建,然后再在相应的文件夹目录下创建文件
File f = new File("somedirname1/somedirname2/somefilename");
if (!f.getParentFile().exists())
    f.getParentFile().mkdirs();//创建文件夹
if (!f.exists())
    f.createNewFile();//创建文件

--------------------------------------------------

File appDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filePath);
if (!appDir.exists()) {
appDir.mkdir();
}
File dir = new File(appDir, formatCurrentDate("yyyy_MM_dd_HH_mm_ss") + ".jpg");
// dir.delete();
// if (!dir.exists()) {
// try {
// dir.createNewFile();
//
// } catch (IOException e) {
// e.printStackTrace();
// }
// LogUtils.e("CommonUtils", "success createFile dir=" + dir.toString());
// return dir;
// } else {
// LogUtils.e("CommonUtils", "success createFile dir=" + dir.toString());
// return dir;
// }
if (!dir.exists()) {
try {
if (!dir.getParentFile().exists()){
dir.getParentFile().mkdirs();
}
if (!dir.exists()){
dir.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
LogUtils.e("CommonUtils", "success createFile dir=" + dir.toString());
return dir;
} else {
LogUtils.e("CommonUtils", "success createFile dir=" + dir.toString());
return dir;
}

Android SDK4/5/6/7,相册、拍照及裁剪功能及遇见的坑的更多相关文章

  1. 手机调用系统的拍照和裁剪功能,假设界面有输入框EditText,在一些手机会出现点击EditText会弹出输入法,却不能输入的情况。

    1. 拍照裁剪后 点击EditText会弹出输入法,却不能输入.可是点击点一EdtiText就能够输入了,所以我就写了一个看不见的EdtiText,切换焦点,这样就攻克了这个奇怪的这问题,应该是and ...

  2. android 开发 实现一个进入相机拍照后裁剪图片或者进入相册选中裁剪图片的功能

    实现思维路径: 以进入相机拍照的思维路线为例子: 1.进入app 2.判断之前是否保存头像,如果有就显示历史图像 (下面代码中在getOldAvatar();方法中执行这个逻辑) 3.点击更换图像的B ...

  3. android使用默认程序进行图片拍照已经裁剪,以及设备读取

    //代码如下: package com.cbsw.yulechangsuo.activity; import java.io.File;import java.io.FileInputStream;i ...

  4. Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载)

    场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将需要滚动查看的照 ...

  5. [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现

    [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现 今天做Android项目的时候要用到图片选择,要实现拍照获取图片和从相册获取图片,并且要求在获取完之后可以裁剪,试了很多方法之 ...

  6. Android 7.0+相机、相册、裁剪适配问题

    Android 7.0+相机.相册.裁剪适配问题 在manifest中: <provider android:name="android.support.v4.content.File ...

  7. Android调用相机实现拍照并裁剪图片,调用手机中的相冊图片并裁剪图片

    在 Android应用中,非常多时候我们须要实现上传图片,或者直接调用手机上的拍照功能拍照处理然后直接显示并上传功能,以下将讲述调用相机拍照处理图片然后显示和调用手机相冊中的图片处理然后显示的功能,要 ...

  8. mui H5+ 调取 相册 拍照 功能 上传图片 + 裁剪功能

    H5+ 相册拍照图片上传 点击用户头像后,弹出actionSheet,选择从相册或是拍照:选取照片后调用上传方法: 上传图片后调用PhotoClip.js  插件进行裁剪 具体流程 弹出actionS ...

  9. Android WebView 实现文件选择、拍照、录制视频、录音

    原文地址:Android WebView 实现文件选择.拍照.录制视频.录音 | Stars-One的杂货小窝 Android中的WebView如果不进行相应的设置,H5页面的上传按钮是无法触发And ...

随机推荐

  1. 总结:独立开发 jar 包组件——功能主要是支持查询数据库的所有表数据

    前言:开发完一个项目,必定总结,这次就将总结记录在博客,第一次开发组件 jar 包,包含前端,后台,中间遇到好多问题,这里一一描述.转载请注明出处: https://www.cnblogs.com/y ...

  2. POJ2478(SummerTrainingDay04-E 欧拉函数)

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16927   Accepted: 6764 D ...

  3. BFC(块状格式化上下文)

    今天先来说关于BFC的一些基础知识 BFC是块状格式化上下文,它是一个独立的渲染区域,规定了内部如何布局,并且这个布局和外部毫不相干 触发BFC的方法 1.根元素(即html) 2.float属性不为 ...

  4. 【代码笔记】iOS-自定义alertView

    一,效果图. 二,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewContro ...

  5. CPA理论与Base理论

    CPA理论: 由于对系统或者数据进行了拆分,我们的系统不再是单机系统,而是分布式系统,针对分布式系统的CAP原理包含如下三个元素. C:Consistency,一致性.在分布式系统中的所有数据 备份, ...

  6. 用eclipse 搭建struts2环境

    一,下载struts2对应的jar包,(http://struts.apache.org/download.cgi#struts2514.1),我一般下载struts2.3版本的 二,打开eclips ...

  7. SQL SERVER 将表字段值0和1互转的几种方法

    需求: 如果表字段的值为 0 则将其修改为1 ,如果表字段的值为 1 则将其修改为 0. 方法一 end 方法二 ) 方法三 )

  8. soapUI 使用soapUI测试http+json协议接口简介

    使用soapUI测试http+json协议接口简介 by:授客 QQ:1033553122 SoapUI-Pro-x64-5.1.2_576025(含破解文件),软件下载地址: http://pan. ...

  9. RecyclerView打造通用的万能Adapter

    既然想做到通用那么现在摆在面前的就三个问题:数据怎么办?布局怎么办? 绑定怎么办?.数据决定采用泛型,布局打算直接构造传递,绑定显示效果肯定就只能回传. 1 基本改造 数据决定采用泛型,布局打算直接构 ...

  10. Android网络编程系列之HTTP协议原理总结

    前言 作为搞移动开发的我们,免不了与网络交互打交道.虽然市面上很多开源库都封装的比较到位,我们实现网络访问也轻车熟路.但还是十分有必要简要了解一下其中的原理,以便做到得心应手,也是通往高级开发工程师甚 ...