Android调用相机并获取原图Path(非缩略图)
来源:互联网 发布:js实现图片的移动 编辑:程序博客网 时间:2024/05/04 06:27
//打开相机
private String picPath = “”;
// 调用系统的拍照功能
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// intent.putExtra("camerasensortype", 2); // 调用前置摄像头
// android.hardware.Camera.open(2);
intent.putExtra("autofocus", true); // 自动对焦
intent.putExtra("fullScreen", false); // 全屏
intent.putExtra("showActionIcons", false);
// 指定调用相机拍照后照片的存储路径
picPath = "IMG_"+System.currentTimeMillis()+".jpg";
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(picPath)));
startActivityForResult(intent, 2);
}
//获取图片PATH
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode = RESULT_OK && requestCode == 2){
File f=new File(picPath);
try {
Uri u = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), null, null));
String picPath_camera = uriToPath(u);
// 实际开发时上传到服务器
// uploadImg(picPath_camera, imgMsgIndex);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
/**
*
* 通过URI获取图片地址
*/
private String uriToPath(Uri imageUri) {
if (context == null || imageUri == null)
return null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, imageUri)) {
if (isExternalStorageDocument(imageUri)) {
String docId = DocumentsContract.getDocumentId(imageUri);
String[] split = docId.split(":");
String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(imageUri)) {
String id = DocumentsContract.getDocumentId(imageUri);
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(imageUri)) {
String docId = DocumentsContract.getDocumentId(imageUri);
String[] split = docId.split(":");
String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
String selection = MediaStore.Images.Media._ID + "=?";
String[] selectionArgs = new String[] { split[1] };
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} // MediaStore (and general)
else if ("content".equalsIgnoreCase(imageUri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(imageUri))
return imageUri.getLastPathSegment();
return getDataColumn(context, imageUri, null, null);
}
// File
else if ("file".equalsIgnoreCase(imageUri.getScheme())) {
return imageUri.getPath();
}
return null;
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
String column = MediaStore.Images.Media.DATA;
String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
- Android调用相机并获取原图Path(非缩略图)
- android调用系统相机拍照之获取原图和缩略图
- android 调用系统相机拍照 获取原图
- android 调用系统相机拍照 获取原图
- android 调用系统相机拍照 获取原图
- android 调用系统相机拍照 获取原图
- android 调用系统相机拍照 获取原图
- android 调用系统相机拍照 获取原图
- android 调用系统相机拍照 获取原图
- Android调用系统相机拍照 获取原图
- Android开发获取相机拍照的原图(并非缩略图)
- Android调用系统相机拍照并显示原图
- 调用Android系统相机拍照并保存原图
- android 调用系统相机拍照并保存照片原图
- 调用Android相机拍照,获取原图的uri
- android调用系统相机,获得原图
- android打开系统相机分别获得原图和缩略图
- 调用系统相机拍照、调用相册、调用系统录像并获取缩略图
- java 反射 InvocationTargetException 笔记
- 第7章 sed、gawk介绍与正则表达式-----------(定义基本正则表达式(BRE)模式)
- 设计师读书笔记连载系列之《设计师要懂心理学》
- JAVA基础入门运算符
- Android 系列 3.4使用Android Studio和JUnit进行测试
- Android调用相机并获取原图Path(非缩略图)
- POJ1852 Ants
- 在PHP中使用协程实现多任务调度
- [题解]BJOI2014
- Android60分钟搞定《FlappyBird》飞扬的小鸟游戏
- RocksDB译文之一 -- RocksDB简介
- 算法-合唱队
- 串口调试工具(Python2.7+pyserial+Tkinter)(很好的Tkinter例子,代码层次结构等)
- lualatex:The basic structure of the book