上传头像 调用相机拍照 ,及简单的调用相册的照片来上传头像
2018-01-05 09:01
513 查看
首先我在对应的主页面 xml里面进行一个相关的UI界面的布局
对应的两个Button按钮 一个拍照,一个调用相册
一个Imageview 的一张小机器人的图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.shangchuantouxiang.MainActivity">
<!-- <com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:placeholderImageScaleType="focusCrop"
fresco:roundAsCircle="true"
/>-->
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="拍照"
android:id="@+id/paizhao"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="相册"
android:id="@+id/xc"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>接下来就是主页面 MainActivity
对应的布局的两个按钮 拍照 和 相册 的点击事件
public class MainActivity extends AppCompatActivity {
Button paizhao, xc;
ImageView img;
//定义一个私有的变量来拍照回调
private String path;
//定义静态变量来记录相册
private static final int IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
paizhao = (Button) findViewById(R.id.paizhao);
xc = (Button) findViewById(R.id.xc);
paizhao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//第一种
// Intent intent = new Intent("android.media.action.STILL_IMAGE_CAMERA");
// startActivity(intent);
// filePath = "storage/emulated/legacy/"+System.currentTimeMillis()+".jpg";
path= Environment.getExternalStorageDirectory().getPath()+"/"+System.currentTimeMillis()+".jpg";
Uri uri=Uri.fromFile(new File(path));//处理存放位置
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//创建拍照的意图
intent .putExtra(MediaStore.EXTRA_OUTPUT, uri);//指定处理方的位置为系统的沼泽的存放位置
startActivityForResult(intent, 1);
}
});
//调用相册的相片回传
xc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,IMAGE);
}
});
}
// 进行的相册里面来获得code来判断 //调用相册
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 调用相机拍照 把相机拍的照传回 该图片的控件
Uri uri = Uri.fromFile(new File(path));
img.setImageURI(uri);
//获取图片路径
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
String imagePath = c.getString(columnIndex);
showImage(imagePath);
c.close();
}
}
private void showImage(String imaePath) {
Bitmap bm = BitmapFactory.decodeFile(imaePath);
img.setImageBitmap(bm);
}
}
这样就可以了
对应的两个Button按钮 一个拍照,一个调用相册
一个Imageview 的一张小机器人的图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.shangchuantouxiang.MainActivity">
<!-- <com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:placeholderImageScaleType="focusCrop"
fresco:roundAsCircle="true"
/>-->
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="拍照"
android:id="@+id/paizhao"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="相册"
android:id="@+id/xc"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>接下来就是主页面 MainActivity
对应的布局的两个按钮 拍照 和 相册 的点击事件
public class MainActivity extends AppCompatActivity {
Button paizhao, xc;
ImageView img;
//定义一个私有的变量来拍照回调
private String path;
//定义静态变量来记录相册
private static final int IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
paizhao = (Button) findViewById(R.id.paizhao);
xc = (Button) findViewById(R.id.xc);
paizhao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//第一种
// Intent intent = new Intent("android.media.action.STILL_IMAGE_CAMERA");
// startActivity(intent);
// filePath = "storage/emulated/legacy/"+System.currentTimeMillis()+".jpg";
path= Environment.getExternalStorageDirectory().getPath()+"/"+System.currentTimeMillis()+".jpg";
Uri uri=Uri.fromFile(new File(path));//处理存放位置
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//创建拍照的意图
intent .putExtra(MediaStore.EXTRA_OUTPUT, uri);//指定处理方的位置为系统的沼泽的存放位置
startActivityForResult(intent, 1);
}
});
//调用相册的相片回传
xc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,IMAGE);
}
});
}
// 进行的相册里面来获得code来判断 //调用相册
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 调用相机拍照 把相机拍的照传回 该图片的控件
Uri uri = Uri.fromFile(new File(path));
img.setImageURI(uri);
//获取图片路径
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
String imagePath = c.getString(columnIndex);
showImage(imagePath);
c.close();
}
}
private void showImage(String imaePath) {
Bitmap bm = BitmapFactory.decodeFile(imaePath);
img.setImageBitmap(bm);
}
}
这样就可以了
相关文章推荐
- android中调用系统的相册或者相机拍照并上传照片
- 很全的demo,包括调用系统相机全屏拍照并保存,访问系统相册,多项保存照片。
- 调用系统相机、相册、剪裁图片并上传(常用于上传头像,兼容Android7.0)
- 【iOS】一个完整的简单的调用系统相机和相册设置头像
- Android调用系统相机拍照、从相册中取照片,裁剪,并保存到手机SD卡中,展示到界面
- Android调用相册或相机拍照选取照片并裁剪
- 调用系统相机拍照,从相册选取图片上传
- Android7.0调用系统相机拍照、读取系统相册照片+CropImageView剪裁照片
- 友盟 PushSDK 3.0集成后,应用中调用相机拍照,相册获取照片 程序会 Crash
- Android7.0调用系统相机拍照、读取系统相册照片+CropImageView剪裁、旋转、翻转照片
- 从相册获取图片及调用相机拍照获取图片,最后上传图片到服务器
- 更换用户头像 调用手机相册或相机拍照 选择图片并裁剪
- iOS 相机调用相册打开及图片头像的上传到服务器详解
- iOS调用系统相册,相机上传头像的基本技巧
- android 调用系统相册选取照片或者打开相机拍照获取图片
- Android中相机拍摄照片,以及相册选择图片压缩上传(压缩后保存进SD中)(可用于修改头像等)
- iOS调用相机,相册,上传头像 分类: ios技术 2015-04-14 11:23 256人阅读 评论(0) 收藏
- Android调用相册或相机拍照选取照片并裁剪
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- iOS调用相机,相册,上传头像