js 调用android 相册调用相机相册选图时怎么跳过裁剪

今天看啥 热点:
private ImageView iv_user_
private String fileName = "";
private File tempF
private int crop = 300;// 裁剪大小
private static final int OPEN_CAMERA_CODE = 10;
private static final int OPEN_GALLERY_CODE = 11;
private static final int CROP_PHOTO_CODE = 12;
private OnClickListener PopupWindowItemOnClick = new OnClickListener() {
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.btn_camera:
initFile();
openCamera();
case R.id.btn_gallery:
initFile();
openGallery();
public void initFile() {
if(fileName.equals("")) {
if(FileUtil.existSDCard()) {
String path = Environment.getExternalStorageDirectory() + File.separator + "JanuBookingOnline" + File.
FileUtil.mkdir(path);
Logger.i("path:" + path);
fileName = path + "user_head_photo.jpg";
tempFile = new File(fileName);
CommonUitl.toast(context, "请插入SD卡");
* 调用相机
public void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 打开相机
intent.putExtra("output", Uri.fromFile(tempFile));
startActivityForResult(intent, OPEN_CAMERA_CODE);
* 打开相册
public void openGallery() {
Intent intent = new Intent(Intent.ACTION_PICK);// 打开相册
intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
intent.putExtra("output", Uri.fromFile(tempFile));
startActivityForResult(intent, OPEN_GALLERY_CODE);
* 裁剪图片
* @param uri
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("output", Uri.fromFile(tempFile));
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", crop);
intent.putExtra("outputY", crop);
startActivityForResult(intent, CROP_PHOTO_CODE);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1)
switch (requestCode) {
case OPEN_CAMERA_CODE:
cropPhoto(Uri.fromFile(tempFile));
case OPEN_GALLERY_CODE:
cropPhoto(data.getData());
case CROP_PHOTO_CODE:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(fileName, options);
if (bitmap != null) {
iv_user_photo.setImageBitmap(bitmap);
CommonUitl.sharedPreferences(context, AppConstants.USER_PHOTO, fileName);
} catch (Exception e) {
e.printStackTrace();
super.onActivityResult(requestCode, resultCode, data);
}650) this.width=650;" src="/uploads/allimg/25B047-0.png" />650) this.width=650;" src="/uploads/allimg/25A919-1.png" />650) this.width=650;" src="/uploads/allimg/.png" />本文出自 “绿兮上弦” 博客,转载请与作者联系!
相关搜索:
相关阅读:
相关频道:
其他教程最近更新【android 调用相机拍照返回图片的求助】_android开发吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:62,359贴子:
【android 调用相机拍照返回图片的求助】收藏
求助:现在用android调用相机拍照,获取返回的图片,并让它放到一个imageview里,要求对图片进行处理,使它的宽度为640,高度为等比例。现在的问题是,无法获得返回图片的宽高,谁会做啊,帮帮忙
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或android开发――从相册中选择图片不裁剪
在郭神的第一行代码中,第8章的从相册中选择图片这块,从相册选一张裁剪后显示到屏幕。但是运行后会发现从相册选了图片后,没有弹出裁剪的界面,直接返回。
查找原因时,发现SD卡路径下的output_image.jpg是一个0字节文件。所以
这张图片没有生成。然后我觉得是向系统发送选择照片的意图出了问题。我好奇的查看了下系统的图库应用(gallery)的清单文件意图过滤器这块,果然有发现:
发现这里用android.intent.action.PICK的action,可以选择图片或是视频。而书里是android.intent.action.GET_CONTENT。
修改后成功跳转到裁剪界面。
再者一个问题,我用
Uri selectedImage = data.getData();//获取系统返回的照片的Uri
输出后发现这并不是一个绝对路径。这对于我们设置到ImageVew上或上传到网络上等 是不正确的。
我这里给出个从相册中选择图片不裁剪的demo,告诉大家如何获得绝对路径,完美运行。
首先新建一个项目,编辑activity_main.xml 文件,在布局中添加一个按钮用于从相册中选择照片,一个imageview。代码如下所示:
data-snippet-id=ext.2f9e1a0d919a8d10c752c253a8a5827c data-snippet-saved=false data-codota-status=done&
然后修改 MainActivity 中的代码,加入从相册选择照片的逻辑,代码如下所示:
package com.example.
import android.app.A
import android.content.I
import android.database.C
import android.graphics.B
import android.graphics.BitmapF
import android.net.U
import android.os.B
import android.provider.MediaS
import android.view.M
import android.view.MenuI
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.ImageV
public class MainActivity extends Activity {
private Button chooseFromA
private ImageView picImageV
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
picImageView= (ImageView)findViewById(R.id.View);
chooseFromAlbum = (Button)findViewById(R.id.choose_from_album);
chooseFromAlbum.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
selectPicture();
* 从相册选择照片(不裁切)
private void selectPicture() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(Intent.ACTION_PICK);//Pick an item from the data
intent.setType(image/*);//从所有图片中进行选择
startActivityForResult(intent, 1);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode==RESULT_OK) {//从相册选择照片不裁切
Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor =getContentResolver().query(selectedImage,
filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
//获取照片路径
cursor.close();
Bitmap bitmap= BitmapFactory.decodeFile(picturePath);
picImageView.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
super.onActivityResult(requestCode, resultCode, data);
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in Manifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return super.onOptionsItemSelected(item);
从 MainActivity可以发现 从相册中选择图片这功能只有两步:
第一步:向发送选择照片的意图。
* 从相册选择照片(不裁切)
private void selectPicture() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(Intent.ACTION_PICK);//Pick an item from the data
intent.setType(image/*);//从所有图片中进行选择
startActivityForResult(intent, 1);
第二步:处理onActivityResult系统返回的结果。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode==RESULT_OK) {//从相册选择照片不裁切
Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor =getContentResolver().query(selectedImage,
filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
//获取照片路径
cursor.close();
Bitmap bitmap= BitmapFactory.decodeFile(picturePath);
picImageView.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
super.onActivityResult(requestCode, resultCode, data);
系统会返回一个选择照片的Uri,Uri selectedImage = data.getData();然后定义String[] filePathColumn = { MediaStore.Images.Media.DATA }; 在filePathColumn 中查找指定Uri路径。通过cursor.getString(columnIndex); 获取照片绝对路径 。private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
private File tempFile = new File(Environment.getExternalStorageDirectory(),
getPhotoFileName());
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用
startPhotoZoom(Uri.fromFile(tempFile));
case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时
// 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同
if (data != null) {
System.out.println("11================");
startPhotoZoom(data.getData());
System.out.println("================");
case PHOTO_REQUEST_CUT:// 返回的结果
if (data != null)
// setPicToView(data);
sentPicToNext(data);
super.onActivityResult(requestCode, resultCode, data);
// 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
调用系统拍照功能:&
Intent cameraintent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(tempFile));
startActivityForResult(cameraintent,
PHOTO_REQUEST_TAKEPHOTO);
调用系统相册功能:&
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
getAlbum.setType("image/*");
startActivityForResult(getAlbum, PHOTO_REQUEST_GALLERY);
调用系统裁剪功能:&
private void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", true);
intent.putExtra("noFaceDetection", true);
System.out.println("22================");
startActivityForResult(intent, PHOTO_REQUEST_CUT);
自定义对话框:&
mDialog = new AlertDialog.Builder(this, R.style.FullScreenDialog)
.create();
if (mDialog != null && !mDialog.isShowing()) {
mDialog.show();
mDialog.setContentView(R.layout.dialog_select_imge);
mDialog.setCanceledOnTouchOutside(false);
自定义style :
&style name="FullScreenDialog" parent="android:style/Theme.Dialog"&
&item name="android:windowNoTitle"&true&/item&
&item name="android:windowFrame"&@null&/item&
&item name="android:windowIsFloating"&true&/item&
&item name="android:windowIsTranslucent"&false&/item&
&item name="android:background"&@android:color/transparent&/item&
&item name="android:windowBackground"&@android:color/transparent&/item&
&item name="android:backgroundDimEnabled"&true&/item&
阅读(...) 评论()

我要回帖

更多关于 android调用本地相册 的文章

 

随机推荐