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

你可能感兴趣的文章
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>