如何拍照,保存并在Android中获取照片
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何拍照,保存并在Android中获取照片相关的知识,希望对你有一定的参考价值。
我一直在搜索一个简单的例子拍照,并使用URI保存它并检索照片进行图像处理,我尝试了很多示例代码,但没有一个顺利进行。
有没有人有示例代码?
答案
定义一个像这样的变量
protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
使用代码从android调用相机。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"fname_" +
String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
并在调用它的类中覆盖onActivityResult函数并输入以下代码。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
//use imageUri here to access the image
Bundle extras = data.getExtras();
Log.e("URI",imageUri.toString());
Bitmap bmp = (Bitmap) extras.get("data");
// here you will get the image as bitmap
}
else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
}
另一答案
有几个示例捕获图像并存储并打开它...
1. Android Camera API - Tutorial
另一答案
我有同样的问题。
我正在测试一些来自互联网的代码,但找不到任何代码。然后,我研究了developer.android的一些基本代码。在那之后,我混合了两个不同的代码,我的一个工作!在这里!
public class MainActivity extends AppCompatActivity {
static final int PICTURE_RESULT = 1;
String mCurrentPhotoPath;
ContentValues values;
private Uri file;
ImageView imageView;
Bitmap help1;
ThumbnailUtils thumbnail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
values = new ContentValues();
}
public void launch_camera(View v) {
// the intent is my camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//getting uri of the file
file = Uri.fromFile(getFile());
//Setting the file Uri to my photo
intent.putExtra(MediaStore.EXTRA_OUTPUT,file);
if(intent.resolveActivity(getPackageManager())!=null)
{
startActivityForResult(intent, PICTURE_RESULT);
}
}
//this method will create and return the path to the image file
private File getFile() {
File folder = Environment.getExternalStoragePublicDirectory("/From_camera/imagens");// the file path
//if it doesn't exist the folder will be created
if(!folder.exists())
{folder.mkdir();}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_"+ timeStamp + "_";
File image_file = null;
try {
image_file = File.createTempFile(imageFileName,".jpg",folder);
} catch (IOException e) {
e.printStackTrace();
}
mCurrentPhotoPath = image_file.getAbsolutePath();
return image_file;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICTURE_RESULT) {
if(resultCode == Activity.RESULT_OK) {
try {
help1 = MediaStore.Images.Media.getBitmap(getContentResolver(),file);
imageView.setImageBitmap( thumbnail.extractThumbnail(help1,help1.getWidth(),help1.getHeight()));
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
XML文件只有一个Button和一个ImageView,不要忘记在你的android清单中声明权限:
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
更多信息建议:https://developer.android.com/training/camera/photobasics.html#TaskPhotoView https://www.youtube.com/watch?v=je9bdkdNQqg
另一答案
请使用以下代码。
package com.example.stackoverflow;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.security.SecureRandom;
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.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
static String str_Camera_Photo_ImagePath = "";
private static File f;
private static int Take_Photo = 2;
private static String str_randomnumber = "";
static String str_Camera_Photo_ImageName = "";
public static String str_SaveFolderName;
private static File wallpaperDirectory;
Bitmap bitmap;
int storeposition = 0;
public static GridView gridview;
public static ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ccccc);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str_SaveFolderName = Environment
.getExternalStorageDirectory()
.getAbsolutePath()
+ "/rajeshsample";
str_randomnumber = String.valueOf(nextSessionId());
wallpaperDirectory = new File(str_SaveFolderName);
if (!wallpaperDirectory.exists())
wallpaperDirectory.mkdirs();
str_Camera_Photo_ImageName = str_randomnumber
+ ".jpg";
str_Camera_Photo_ImagePath = str_SaveFolderName
+ "/" + str_randomnumber + ".jpg";
System.err.println(" str_Camera_Photo_ImagePath "
+ str_Camera_Photo_ImagePath);
f = new File(str_Camera_Photo_ImagePath);
startActivityForResult(new Intent(
MediaStore.ACTION_IMAGE_CAPTURE).putExtra(
MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)),
Take_Photo);
System.err.println("f " + f);
}
});
}
// used to create randon numbers
public String nextSessionId() {
SecureRandom random = new SecureRandom();
return new BigInteger(130, random).toString(32);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Take_Photo) {
Stri以上是关于如何拍照,保存并在Android中获取照片的主要内容,如果未能解决你的问题,请参考以下文章
Android笔记67Android之使用系统中的相机功能(拍照保存照片显示拍摄的照片照片保存到图库等操作)