#####1、界面截图
#####2、layout 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lumeng.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/bt_camera"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="@string/camera"
android:layout_weight="1"
android:textSize="40dp"/>
<Button
android:id="@+id/bt_picture"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="@string/picture"
android:layout_weight="1"
android:textSize="40dp"/>
<Button
android:id="@+id/bt_share"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="@string/share"
android:layout_weight="1"
android:textSize="40dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="100dp"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" />
</LinearLayout>
</RelativeLayout>
#####3、主要功能实现
package com.example.lumeng.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.icu.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button bt_cam,bt_pict,bt_share;
ImageView imv;
Uri imgUri; //拍照存盘的Uri对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_cam = (Button) findViewById(R.id.bt_camera);
bt_pict = (Button) findViewById(R.id.bt_picture);
bt_share = (Button) findViewById(R.id.bt_share);
imv = (ImageView) findViewById(R.id.img_view);
bt_cam.setOnClickListener(this);
bt_pict.setOnClickListener(this);
bt_share.setOnClickListener(this);
//设置屏幕不随手机旋转
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
//设置屏幕直向显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.bt_camera:
//获取系统的公用图像文件路径
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
String fname = "P" + System.currentTimeMillis() + ".jpg";//利用时间组合出不同文件名
//String fname = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";//"当前时间年月日_时分秒.jpg"
imgUri = Uri.parse("file://" + dir + "/" + fname);//按照前面的路径和文件名创建Uri对象
Intent it_cam = new Intent("android.media.action.IMAGE_CAPTURE");//创建拍照动作
it_cam.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);//将uri加到拍照Intent 的额外数据中
startActivityForResult(it_cam, 100);//启动Intent 并要求返回数据
break;
case R.id.bt_picture:
//动作设为“选取内容”
Intent it_pict = new Intent(Intent.ACTION_GET_CONTENT);
//设置要选取的媒体类型为:所有的图片
it_pict.setType("image/*");
//启动 Intent,并要求返回选取的图片文件
startActivityForResult(it_pict,101);
case R.id.bt_share:
//选择合适的intent 将图片分享出去 ,比如短信 蓝牙 QQ 等
if (imgUri != null) {
Intent it_share = new Intent(Intent.ACTION_SEND);
it_share.setType("image/*");
it_share.putExtra(Intent.EXTRA_STREAM,imgUri);
startActivity(it_share);
}
}
}
Uri convertUri (Uri uri) {
//如果是以“content”开头
if (uri.toString().substring(0,7).equals("content")) {
String [] colName = {MediaStore.MediaColumns.DATA};//生命要查询的字段
//以uri进行查询
Cursor cursor = getContentResolver().query(uri,colName,null,null,null);
cursor.moveToFirst();//一道查询结果的地一个记录
uri = Uri.parse("file://" + cursor.getString(0));//将路径转为Uri
cursor.close();//关闭查询结果
}
return uri; //返回 uri 对象
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK ) {
switch (requestCode) {
case 100: //拍照
//设为系统共享媒体文件
Intent it = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,imgUri);
sendBroadcast(it);
break;
case 101://选取图片
//获取图片的uri并进行uri格式转换,转换成“file://”格式
imgUri = convertUri(data.getData());
}
showImg();//显示照片
}
else {
Toast.makeText(this,"没有拍到照片", Toast.LENGTH_LONG).show();
}
}
private void showImg() {
int iw,ih, vw, vh; //照片宽高,组件宽高
boolean needRotate; //用来存储是否需要旋转
BitmapFactory.Options option = new BitmapFactory.Options(); //创建选项对象
option.inJustDecodeBounds = true; //设置选项: 只读取图像文件信息而不载入图像文件
BitmapFactory.decodeFile(imgUri.getPath(),option); //读取图像文件信息存入Option 中
iw = option.outWidth; //从option中读取图像文件宽度
ih =option.outHeight;//从option中读取图像文件高度
vw = imv.getWidth();//获取组建 ImgeView 的宽度
vh = imv.getHeight();//获取组建Imgview的高度
int scaleFactor;
if (iw < ih) { //如果图片的宽度小于高度
needRotate = false; //不需要旋转
scaleFactor = Math.min(iw/vw,ih/vh); //计算缩小比例
}
else {
needRotate = true; //需要旋转
scaleFactor = Math.min(iw/vw,ih/vh); //计算缩小比例
}
option.inJustDecodeBounds = false; //关闭只加载图像文件信息的选项
option.inSampleSize = scaleFactor; //设置缩小比例 例如3 ,则长宽都将缩小 1/3
Bitmap bmp = BitmapFactory.decodeFile(imgUri.getPath(),option); //载入图片文件
if (needRotate) { //如果需要旋转
Matrix matrix = new Matrix(); //创建 Matrix 对象
matrix.postRotate(90);//设置旋转角度(顺时针90度)
//用原来图片生成新的图片
bmp = Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);
}
imv.setImageBitmap(bmp); //显示图片
//显示图片同时打印图片信息
new AlertDialog.Builder(this)
.setTitle("图片文件信息")
.setMessage("图像文件路径:" + imgUri.getPath() +
"\n 原始尺寸:" + iw + "x" + ih +
"\n 载入尺寸:" + bmp.getWidth() + "x" + bmp.getHeight() +
"\n 显示尺寸:" + vw + "x" + vh)
.setNegativeButton("关闭",null)
.show();
}
}
#####4、相关权限设计
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lumeng.myapplication">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
文献参考:
android app开发入门 施威铭 编著
本人郑重声明,本博客所著文章、图片版权归权利人持有,本博只做学习交流分享所用,不做任何商业用途。访问者可將本博提供的內容或服务用于个人学习、研究或欣赏,不得用于商业使用。同時,访问者应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人的合法权利;如果用于商业用途,须征得相关权利人的书面授权。若以上文章、图片的原作者不愿意在此展示內容,请及时通知在下,將及时予以刪除。