我一直在寻找以下问题三天
我的代码如下
private void openCamera () {
try {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName = Constants.IMAGE_CAPTURE_FILE_NAME_PREFIX + System.currentTimeMillis() + Constants.IMAGE_FILE_EXT_JPG;
mImageFile = new File(FileManager.getInstance().getFileFullName(Constants.IMAGE_FOLDER, fileName));
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageFile);
startActivityForResult(captureIntent, Constants.ACTIVITY_CAMERA_REQUEST_CODE);
} catch(ActivityNotFoundException anfe) {
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast toast = Toast.makeText(ImagesSelectorActivity.this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
public String getFileFullName(String folderName, String fileName) {
String filepath;
if (PhoneUtils.isSDCardFound() == true)
filepath = Environment.getExternalStorageDirectory().getPath();
else
filepath = Environment.getDataDirectory().getPath();
File file = new File(filepath, folderName);
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + fileName);
}
关于活动结果
if(requestCode == Constants.ACTIVITY_CAMERA_REQUEST_CODE) {
Uri picUri = data.getData();
if (picUri == null) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Uri tempUri = PhoneUtils.getBitmapUri(getApplicationContext(), bitmap);
Log.i("AMIRA", tempUri.toString());
}
performPreview(picUri);
}
图像未保存在我的文件夹中的问题,以及返回的网址是缩略图uri
我需要在我需要的路径中保存捕获后的图像,并能够读取完整尺寸的图像。
我无法在设备的任何位置找到从相机捕获的图像。
答案 0 :(得分:1)
我使用与你的工作几乎相同的工艺,工作正常。请尝试执行以下操作:
在打开的相机部分,而不是像这样发送File
发送Uri
:
mImageUri = Uri.fromFile(mImageFile); //keep mImageUri as member so that you can use it on the activity result.
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
之后在onActivityResult
中执行此操作:
if (mImageUri != null && !mImageUri.getPath().isEmpty()) {
performPreview(mImageUri);
}
希望这有帮助!
答案 1 :(得分:0)
类文件如下:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture">
</Button>
<ImageView
android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_width="wrap_content">
</ImageView>
在清单文件中添加权限:
<uses-feature android:name="android.hardware.camera"></uses-feature>
希望这会有所帮助......