我有一个代码,您可以在其中拍照,然后将其保存为1.jpg,2.jpg,3.jpg。问题是我不知道如何在第二个活动中显示拍摄的图像。
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg
// and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
}
catch (IOException e)
{
}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
}
}
}
如果我试图将我的形象作为parcable传递,那么质量非常差,这就是我不想要的。有人可以帮我这个,或者给一个好的指导?最重要的是我不想在我的第一个活动中显示它,我想在我的第二个活动中显示它,调用1.jpg的路径。谢谢!
答案 0 :(得分:1)
方法是将拍摄的照片写入文件并获取所述文件的路径并将路径传递给下一个活动,并使用所述路径文件和设置图像视图初始化位图...生成一个代码不久
在我发布一个片段的同时调查here 编辑:SNIPPET 此代码设置相机拍照并保存到文件中。
private void onCameraSelected() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, PICTURE_CAMERA);
}
private Uri getOutputMediaFileUri() {
return Uri.fromFile(getOutputMediaFile());
}
private File getOutputMediaFile() {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
onActivityResult上的检测摄像机的结果代码/请求代码,并按照这样的方式使用它
private void loadImageFromCamera(Intent data) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger images
// options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);
setLoadedImage(bitmap);// set bitmap to imageview here.
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IOException e) {
Logger.logError("File not found :" + fileUri.getPath());
e.printStackTrace();
}
}
基本上你正在使用变量String filePath = fileUri.getPath();
//只是在活动级别或其他事件中声明Uri fileUri。
答案 1 :(得分:1)
You will need to pass the camera intent a location to save the image. You then have to retrieve the image from the phones storage in order to display it in an image view.
set a member variable to keep track of the image:
private Uri mImageUir;
Then to request a camera intent:
File photo = null;
try {
// place where to store camera taken picture
photo = createTemporaryFile("picture", ".jpg");
photo.delete();
} catch (Exception e) {
e.printStackTrace();
}
mImageUir = Uri.fromFile(photo);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUir);
activity.startActivityForResult(intent, TAKE_PHOTO);
...
private File createTemporaryFile(String part, String ext) throws Exception
{
File tempDir= Environment.getExternalStorageDirectory();
tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
if(!tempDir.exists())
{
tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
}
Then in your OnActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK) {
//firstly find correct orientation of photo
ExifInterface exif = null;
Integer photoOrientation = null;
try {
exif = new ExifInterface(mImageUir.getPath());
photoOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap photo;
ContentResolver resolver = getContext().getContentResolver();
try {
//get the image file
photo = MediaStore.Images.Media.getBitmap(resolver, mImageUir);
//You should scale it down here if you dont need in full size
//rotate to correct orientation
if(photoOrientation != null) {
photo = rotateBitmap(photo, photoOrientation);
}
mYourImageView.setImageBitmap(photo);
//delete temporary image file (if you need to)
}
catch (Exception e){
e.printStackTrace();
}
}
}
and here is my method to rotate the image:
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
}
catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}