您的位置:首页 > 其它

上传头像 调用相机拍照 ,及简单的调用相册的照片来上传头像

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);

}

}

这样就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐