android实现拍照以及上传
来源:互联网 发布:python源码 编辑:程序博客网 时间:2024/05/21 07:54
最近看了几篇关于android照相机的一些文章,现在总结如下,直接上源代码把,该说的都用注释说完了
1.java代码
package org.android.test;import java.io.File;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class Android_mytestActivity extends Activity {/** Called when the activity is first created. */// 定义一个button打开照相机,定义一个imageview显示照相机所拍摄的相片;Button but,upload_image;ImageView img;// 获取sd卡根目录地址,并创建图片父目录文件对象和文件的对象;String file_str = Environment.getExternalStorageDirectory().getPath();File mars_file = new File(file_str + "/my_camera");File file_go = new File(file_str + "/my_camera/file.jpg");@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);but = (Button) findViewById(R.id.my_camare_button);upload_image=(Button)findViewById(R.id.upload_image);img = (ImageView) findViewById(R.id.my_img_view);//拍照but.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 验证sd卡是否正确安装:if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {// 先创建父目录,如果新创建一个文件的时候,父目录没有存在,那么必须先创建父目录,再新建文件。if (!mars_file.exists()) {mars_file.mkdirs();}/*//常规情况下,我们这里会 创建子目录,但在这里不用系统拍照完毕后会根据所给的图片路径自动去实现;if(!file_go.exists()){try {file_go.createNewFile();} catch (IOException e) {}}*/// 设置跳转的系统拍照的activity为:MediaStore.ACTION_IMAGE_CAPTURE ;Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 并设置拍照的存在方式为外部存储和存储的路径;intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file_go));//跳转到拍照界面;startActivityForResult(intent, 0x1);} else {Toast.makeText(Android_mytestActivity.this, "请先安装好sd卡",Toast.LENGTH_LONG).show();}}});//上传upload_image.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(file_go.exists()){//验证图片存在后就实现,上传功能,得到与服务器的输出流...//什么URLconnection ,HttpURLconnectio等都可以.......Toast.makeText(Android_mytestActivity.this, "上传中....",Toast.LENGTH_LONG).show();}else{Toast.makeText(Android_mytestActivity.this, "请先拍照....",Toast.LENGTH_LONG).show();}}});}//拍照结束后显示图片;@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stub// 判断请求码和结果码是否正确,如果正确的话就在activity上显示刚刚所拍照的图片;if (requestCode == 0x1 && resultCode == this.RESULT_OK) {/*使用BitmapFactory.Options类防止OOM(Out Of Memory)的问题;创建一个BitmapFactory.Options类用来处理bitmap;*/BitmapFactory.Options myoptions=new BitmapFactory.Options();/*设置Options对象inJustDecodeBounds的属性为true,用于在BitmapFactory的decodeFile(String path, Options opt)后获取图片的高和宽;而且设置了他的属性值为true后使用BitmapFactory的decodeFile()方法无法返回一张图片的bitmap对象,仅仅是把图片的高和宽信息给Options对象;*/myoptions.inJustDecodeBounds=true; BitmapFactory.decodeFile(file_go.getAbsolutePath(),myoptions);//根据在图片的宽和高,得到图片在不变形的情况指定大小下的缩略图,设置宽为222; int height=myoptions.outHeight*222/myoptions.outWidth;myoptions.outWidth=222;myoptions.outHeight=height;//在重新设置玩图片显示的高和宽后记住要修改,Options对象inJustDecodeBounds的属性为false;//不然无法显示图片;myoptions.inJustDecodeBounds=false;//还没完这里才刚开始,要节约内存还需要几个属性,下面是最关键的一个;myoptions.inSampleSize=myoptions.outWidth/222;//还可以设置其他几个属性用于缩小内存;myoptions.inPurgeable=true;myoptions.inInputShareable=true;myoptions.inPreferredConfig=Bitmap.Config.ARGB_4444;// 默认是Bitmap.Config.ARGB_8888//成功了,下面就显示图片咯;Bitmap bitmat = BitmapFactory.decodeFile(file_go.getAbsolutePath(),myoptions);img.setImageBitmap(bitmat);} else {System.out.println("不显示图片");}super.onActivityResult(requestCode, resultCode, data);}}
2.布局文件
<?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:background="#aaaaaa" android:orientation="vertical" > <Button android:id="@+id/my_camare_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击拍照" /> <Button android:id="@+id/upload_image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="上传相片" /> <ImageView android:id="@+id/my_img_view" android:layout_width="wrap_content" android:layout_height="fill_parent" /> </LinearLayout>
上面已经实现拍照并显示了,可以在ImageView旁边用放一个控件,用来美化相片,再上传效果更好,哈哈。
3.主配置文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.android.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <!-- <!– 对SD卡进行写权限 –> --> <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=".Android_mytestActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
注意一定要加关于文件读写的权限,有个权限我没有加(就是照相机的):
<uses-permission android:name="android.permission.CAMERA" />
因为我们是跳转的系统的照相机的activity,所以关于拍照仍然是系统完成的,因此这里不需要加上这个权限;
- android实现拍照以及上传
- Android实现拍照并上传
- Android实现从手机相册上传头像以及拍照上传到服务器
- android拍照上传和本地上传功能实现了
- Android 拍照 以及从本地选择图片 上传
- Android 拍照 以及从本地选择图片 上传
- h5实现拍照上传
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照上传照片
- android 拍照和上传
- android 拍照上传照片
- android 拍照上传照片
- android拍照上传
- 第四次c程序上机报告
- 2013.3.23 腾讯马拉松第二届第四题 医生体检
- Android TabHost的使用
- android入门:zxing学习笔记(二)
- mysql 乱码问题解决
- android实现拍照以及上传
- 如何在程序中实现关联指定的文件类型
- Android Path Button效果实现
- HR不会告诉你的十件事
- vc++高级班之注册表篇[4]---其他类型键值的设置
- 编程统计PI前100位中0-9每个数出现的次数
- Day 19 list(frequency 4)
- Day 20 (frequency 3)
- 输出本地机器的时间Examole4_13(改)