博客
关于我
android拍照和本地选择图片
阅读量:481 次
发布时间:2019-03-06

本文共 3077 字,大约阅读时间需要 10 分钟。

注意:本程序是我自己写的,测试过的,可以直接运行,完全适合新手小白,是非常适合学习的实例,下载地址在下方、

新手需要注意,在AndroidManifest.xml文件中需要添加权限,否则无法程序会报错

权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.INTERNET" />

下载地址:http://download.csdn.net/detail/cf8833/9283371

MainActivity.java 

package com.example.zmap;

 
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button bn_photo;
private Button bn_check;
private ImageView iv_photo;
private Intent intent;
private final int OPEN_RESULT = 1; // 打开照相机
private final int PICK_RESULT = 2; // 选择图片库
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn_photo = (Button) findViewById(R.id.bn_photo);
bn_check = (Button) findViewById(R.id.bn_check);
iv_photo = (ImageView) findViewById(R.id.iv_photo);
bn_photo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, OPEN_RESULT);
}
});
bn_check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_RESULT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case OPEN_RESULT:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap)bundle.get("data");
iv_photo.setImageBitmap(bitmap);
}
break;
case PICK_RESULT:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
iv_photo.setImageURI(uri);
}
break;
default:
break;
}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/bn_photo"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="拍照" />
        <Button
            android:id="@+id/bn_check"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="本地" />
    </LinearLayout>
    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

转载地址:http://lspdz.baihongyu.com/

你可能感兴趣的文章
MongoDB学习笔记(8)--索引及优化索引
查看>>
ms sql server 2008 sp2更新异常
查看>>
MS UC 2013-0-Prepare Tool
查看>>
msbuild发布web应用程序
查看>>
MSCRM调用外部JS文件
查看>>
MSEdgeDriver (Chromium) 不适用于版本 >= 79.0.313 (Canary)
查看>>
msf
查看>>
MSSQL数据库查询优化(一)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>