-
调用android系统相机拍照并保存
- <span style="font-size:18px;">package com.demo;
-
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Calendar;
- import java.util.Locale;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.text.format.DateFormat;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.Toast;
-
- public class MyCameraActivity extends Activity {
-
- private Button button;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
-
- startActivityForResult(intent, 1);
- }
- });
- }
-
- @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)) {
- Log.i("TestFile",
- "SD card is not avaiable/writeable right now.");
- return;
- }
- String name = new DateFormat().format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg";
- Toast.makeText(this, name, Toast.LENGTH_LONG).show();
- Bundle bundle = data.getExtras();
- Bitmap bitmap = (Bitmap) bundle.get("data");
-
- FileOutputStream b = null;
-
- File file = new File("/sdcard/myImage/");
- file.mkdirs();
- String fileName = "/sdcard/myImage/"+name;
-
- try {
- 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);
- }
- }
- }
- </span>
-
相关阅读:
总结前端笔面试遇到的问题——HTML部分
js中的克隆方法
js操作cookie
JS中4种常见的内存泄漏
html、css、js三者的加载顺序
js中object定义的几种方法
关于settimeout的面试题
css各种居中解决方法
JS实现继承的几种方式
将博客搬至CSDN
-
原文地址:https://www.cnblogs.com/xgjblog/p/4193472.html
Copyright © 2020-2023
润新知