Android 系统相机的调用以及获取图片
来源:互联网 发布:马甲线软件 编辑:程序博客网 时间:2024/04/30 03:50
package com.mx.test.last;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class CreamActivity extends Activity {
private View nameView;
private View imglay;
private EditText editText;
private String name;
private Bitmap bitmap = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button camButton = (Button) findViewById(R.id.test);
camButton.setOnClickListener(new Oncliklisener());
nameView = findViewById(R.id.namelay);
nameView.setVisibility(View.GONE);
imglay = findViewById(R.id.imglay);
editText = (EditText) findViewById(R.id.name_text);
Button nameButton = (Button) findViewById(R.id.name_button);
nameButton.setOnClickListener(new Oncliklisener());
}
private final class Oncliklisener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.test:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动相机
startActivityForResult(intent, 1);
break;
case R.id.name_button:
name = editText.getText().toString();
//禁用系统软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
showImg(bitmap, name);
imglay.setVisibility(View.VISIBLE);// 显示View
nameView.setVisibility(View.GONE);// 隐藏View
break;
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测SD卡是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
Bundle bundle = data.getExtras();
bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
File file = new File("/sdcard/myImage/");
file.mkdirs();// 创建文件夹
imglay.setVisibility(View.GONE);
nameView.setVisibility(View.VISIBLE);
}
}
private void showImg(Bitmap bitmap, String name) {
FileOutputStream b = null;
try {
String fileName = "/sdcard/myImage/" + name + ".jpg";
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);// 将图片显示在ImageView里
}
}
main.mxl
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="@+id/namelay"
layout="@layout/name"
/>
<LinearLayout
android:id="@+id/imglay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text" />
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#999999" />
</LinearLayout>
</LinearLayout>
配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mx.test.last"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".CreamActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Test_Name"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
被引用name.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="wrap_content"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:text="@string/write"
android:id="@+id/name_text"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yes"
android:id="@+id/name_button"
/>
</LinearLayout>
- Android 系统相机的调用以及获取图片
- android获取gallery中的图片以及调用系统相机
- Android调用系统的相机,图库,以及裁剪图片功能。
- Android 调用相机以及调用系统图片软件浏览图片
- Android手机调用系统相机拍照、裁剪以及获取Url上传图片
- android 调用相机获取图片
- Android调用系统相机拍照并获取图片
- android 调用系统相机并获取图片路径
- Android调用系统相册或相机获取图片
- Android调用系统图库和相机获取图片并裁剪
- 调用系统相机/相册获取图片的完美解决方案
- Android打开系统相机、相册,并进行裁剪图片的工具类CameraUtil,以及相册获取图片注意要点。
- Android中调用系统相机、系统相册来获取图片,并裁剪图片。
- android调用系统(相机)的图片,并且返回
- android调用相机获取高清图片
- android 调用相机和获取相册图片
- android调用系统相机,获取照片的适配性问题
- android调用系统相机,获取照片的适配性问题
- gitosis/git服务器搭建
- hdu 1575
- Floyd算法
- Html position(static | absolute | fixed | relative)定位
- java处理XML三种解析方式比较
- Android 系统相机的调用以及获取图片
- Uva 11374 - Airport Express 最短路
- 【拆了CSS照样是页面】之——HTML语义化(含H5)
- The Broken Window Theory
- 浅谈字符编码
- HDU2072:单词数
- Linux下安装jboss并设置自启动服务
- 包含min函数的栈
- Light oj 1070 - Algebraic Problem