博客
关于我
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/

你可能感兴趣的文章
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>