我正在开发一个应用程序,我必须创建一个摄像头并保存捕获的图像。 我写的代码在这里:
package com.dev.camera;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button cambutton;
ImageView photoImage = null;
private static final String TAG = "CallCamera";
private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
Uri fileUri = null;
//ImageView photoImage1 = null;
private File getOutputPhotoFile() {
File directory = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), getPackageName());
if (!directory.exists()) {
if (!directory.mkdirs()) {
Log.e(TAG, "Failed to create storage directory.");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.US).format(new Date());
return new File(directory.getPath() + File.separator + "IMG_"
+ timeStamp + ".jpg");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button camButton = (Button)findViewById(R.id.btncam);
ImageView photoImage = (ImageView)findViewById(R.id.iv);
camButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = Uri.fromFile(getOutputPhotoFile());
i.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
if (resultCode == RESULT_OK) {
Uri photoUri = null;
if (data == null) {
// A known bug here! The image should have saved in fileUri
Toast.makeText(this, "Image saved successfully",
Toast.LENGTH_LONG).show();
photoUri = fileUri;
} else {
photoUri = data.getData();
Toast.makeText(this, "Image saved successfully in: " + data.getData(),
Toast.LENGTH_LONG).show();
}
showPhoto(photoUri.getPath());
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Callout for image capture failed!",
Toast.LENGTH_LONG).show();
}
}
}
private void showPhoto(String photoUri) {
File imageFile = new File (photoUri);
if (imageFile.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
photoImage.setImageDrawable(drawable);
}
}
}
这是我用于此应用程序的xml文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="${relativePackage}.${activityClass}"
android:background="#858500">
<Button
android:id="@+id/btncam"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="take picture"
android:gravity="center"/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_callcamera"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
当我运行此代码时,我收到一个无法创建存储目录的错误。如果有人可以帮助我,我将非常感激。
答案 0 :(得分:0)
使用此代码..我在您的文件中进行了一些更改..
package com.example.sampleapp;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button cambutton;
ImageView photoImage = null;
private static final String TAG = "CallCamera";
private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
Uri fileUri = null;
private File getOutputPhotoFile() {
File directory = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), getPackageName());
if (!directory.exists()) {
if (!directory.mkdirs()) {
Log.e(TAG, "Failed to create storage directory.");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.US).format(new Date());
return new File(directory.getPath() + File.separator + "IMG_"
+ timeStamp + ".jpg");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button camButton = (Button)findViewById(R.id.btncam);
photoImage = (ImageView)findViewById(R.id.iv);
camButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = Uri.fromFile(getOutputPhotoFile());
i.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ)
{
if (resultCode == RESULT_OK) {
Uri photoUri = null;
if (data == null) {
// A known bug here! The image should have saved in fileUri
Toast.makeText(this, "Image saved successfully",Toast.LENGTH_LONG).show();
photoUri = fileUri;
showPhoto(photoUri);
} else {
photoUri = data.getData();
showPhoto(photoUri);
Toast.makeText(this, "Image saved successfully in: " + data.getData(),
Toast.LENGTH_LONG).show();
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Callout for image capture failed!",
Toast.LENGTH_LONG).show();
}
}
}
private void showPhoto(Uri photoUri) {
File imageFile = new File(photoUri.getPath());
if (imageFile.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
photoImage.setImageDrawable(drawable);
}
}
}
这是你的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="${relativePackage}.${activityClass}"
android:background="#858500">
<Button
android:id="@+id/btncam"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="take picture"
android:gravity="center"/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btncam"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
将这些权限添加到清单文件:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />