[英]Camera picture from storage to imageview?
I have a code, where you can take a picture, and then save it like 1.jpg,2.jpg,3.jpg. 我有一个代码,您可以在其中拍照,然后将其保存为1.jpg,2.jpg,3.jpg。 The problem is that I have no idea how to display the taken image in my second activity.
问题是我不知道如何在第二个活动中显示拍摄的图像。
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");
}
}
}
If I try to pass my image as parcable, the quality is pretty bad, that's what I don't want. 如果我尝试将图像传递为可解析的图像,那么质量会很差,这就是我所不想要的。 Anybody can help me with this, or show a good guide?
任何人都可以帮助我解决这个问题,或者显示一个好的指南? The most important is that I don't want to display it in my first activity, I want to display it in my second activity, calling the path of the 1.jpg.
最重要的是,我不想在第一个活动中显示它,而是想在第二个活动中显示它,调用1.jpg的路径。 Thanks!
谢谢!
The way is to write the picture taken to a file and get the path to said file and passing the path to the next activity and intializing a bitmap using said path to file and setting the image view...ill post a code soon 方法是将拍摄的图片写入文件并获取该文件的路径,并将该路径传递到下一个活动,并使用该文件路径初始化位图并设置图像视图...很快就会发布代码
mean while look into here while i post a snippet EDIT: SNIPPET this code sets up camera to take a picture and save to a file. 意思是,当我发布摘要时,请看这里。编辑:快照此代码设置相机拍照并保存到文件。
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;
}
on the onActivityResult detect resultcode/request code for camera and consume it like so 在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();
}
}
Basically youre playing with the variable String filePath = fileUri.getPath();
基本上,您在玩变量
String filePath = fileUri.getPath();
// just declare Uri fileUri in activity level or something. //只需在活动级别中声明Uri fileUri即可。
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
然后在您的
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.