从相机拍摄全尺寸照片
Take full size photo from camera
我根据@CommonWare 的建议更新了我的代码。
private static final String EXTRA_FILENAME = "qamatris.novi.com.tr.EXTRA_FILENAME";
private static final String FILENAME = UUID.randomUUID().toString().replaceAll("-", "");;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(EXTRA_FILENAME, output);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 4) {
Uri uri =Uri.fromFile(output);
Bitmap photo = getScaledBitmapFromUri(ctx, uri);
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream2);
byte[] resarray = stream2.toByteArray(); }
***How to call camera intent***
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (savedInstanceState==null) {
File dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
dir.mkdirs();
output=new File(dir, FILENAME);
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
if (output.exists()) {
output.delete();
}
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
现在我得到 NullpointerException:file“Uri uri =Uri.fromFile(output);”因为输出为空。
这一行(输出=(文件)savedInstanceState.getSerializable(EXTRA_FILENAME);)
So what im doing wrong ?
您设置了EXTRA_OUTPUT
,然后忽略了它。您的照片可能位于您提供给 EXTRA_OUTPUT
的位置。我说 "may" 是因为您正在使用 ACTION_IMAGE_CAPTURE
请求随机第三方应用拍照,而此类应用有时会出现错误。
这个 activity,来自 this sample app,使用 ACTION_IMAGE_CAPTURE
拍照,然后要求第三方应用程序使用 ACTION_VIEW
查看生成的照片:
/***
Copyright (c) 2008-2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final String EXTRA_FILENAME=
"com.commonsware.android.camcon.EXTRA_FILENAME";
private static final String FILENAME="CameraContentDemo.jpeg";
private static final int CONTENT_REQUEST=1337;
private File output=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (savedInstanceState==null) {
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
dir.mkdirs();
output=new File(dir, FILENAME);
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
if (output.exists()) {
output.delete();
}
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(EXTRA_FILENAME, output);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/jpeg");
startActivity(i);
finish();
}
}
}
}
请注意,我在 Uri
到 ACTION_VIEW
中使用的值与我在 EXTRA_OUTPUT
extra 中使用的值相同。
为 onActivityResult()
尝试此代码,希望它能起作用。
您可以通过targetUri
获取相机拍摄的全尺寸照片
首先,当您的代码创建图像文件时,您必须获取 cameraImageUri。
使用它:
cameraImageUri = Uri.fromFile(createImageFile());
然后你必须获得意图回调 onActivityResult()
并执行此操作:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_PIC_REQUEST || requestCode == GALLERY_PIC_REQUEST){
if(resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_PIC_REQUEST) {
targetUri = cameraImageUri;
}
else{
targetUri = data.getData();
}
Log.i(TAG, "Image path: " + targetUri);
if(targetUri != null) {
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
Bitmap photo = getScaledBitmapFromUri(getContext(), targetUri);
imageView.setImageBitmap(photo);
} else {
CommonUtilities.toastShort(getContext(), "Unable to retrieve image, please retake...");
}
}
}
}
public static Bitmap getScaledBitmapFromUri(Context context, Uri uriImageFile) {
Bitmap scaledBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 4;
try {
BitmapFactory.decodeStream(context.getContentResolver()
.openInputStream(uriImageFile), null, options);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
return null;
}
int srcWidth = options.outWidth;
int scale = 1;
while (srcWidth / 2 > 60) {
srcWidth /= 2;
scale *= 2;
}
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = scale;
try {
scaledBitmap = BitmapFactory.decodeStream(context
.getContentResolver().openInputStream(uriImageFile), null, options);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return scaledBitmap;
}
我根据@CommonWare 的建议更新了我的代码。
private static final String EXTRA_FILENAME = "qamatris.novi.com.tr.EXTRA_FILENAME";
private static final String FILENAME = UUID.randomUUID().toString().replaceAll("-", "");;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(EXTRA_FILENAME, output);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 4) {
Uri uri =Uri.fromFile(output);
Bitmap photo = getScaledBitmapFromUri(ctx, uri);
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream2);
byte[] resarray = stream2.toByteArray(); }
***How to call camera intent***
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (savedInstanceState==null) {
File dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
dir.mkdirs();
output=new File(dir, FILENAME);
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
if (output.exists()) {
output.delete();
}
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
现在我得到 NullpointerException:file“Uri uri =Uri.fromFile(output);”因为输出为空。 这一行(输出=(文件)savedInstanceState.getSerializable(EXTRA_FILENAME);)
So what im doing wrong ?
您设置了EXTRA_OUTPUT
,然后忽略了它。您的照片可能位于您提供给 EXTRA_OUTPUT
的位置。我说 "may" 是因为您正在使用 ACTION_IMAGE_CAPTURE
请求随机第三方应用拍照,而此类应用有时会出现错误。
这个 activity,来自 this sample app,使用 ACTION_IMAGE_CAPTURE
拍照,然后要求第三方应用程序使用 ACTION_VIEW
查看生成的照片:
/***
Copyright (c) 2008-2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final String EXTRA_FILENAME=
"com.commonsware.android.camcon.EXTRA_FILENAME";
private static final String FILENAME="CameraContentDemo.jpeg";
private static final int CONTENT_REQUEST=1337;
private File output=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (savedInstanceState==null) {
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
dir.mkdirs();
output=new File(dir, FILENAME);
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
if (output.exists()) {
output.delete();
}
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(EXTRA_FILENAME, output);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/jpeg");
startActivity(i);
finish();
}
}
}
}
请注意,我在 Uri
到 ACTION_VIEW
中使用的值与我在 EXTRA_OUTPUT
extra 中使用的值相同。
为 onActivityResult()
尝试此代码,希望它能起作用。
您可以通过targetUri
首先,当您的代码创建图像文件时,您必须获取 cameraImageUri。 使用它:
cameraImageUri = Uri.fromFile(createImageFile());
然后你必须获得意图回调 onActivityResult()
并执行此操作:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_PIC_REQUEST || requestCode == GALLERY_PIC_REQUEST){
if(resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_PIC_REQUEST) {
targetUri = cameraImageUri;
}
else{
targetUri = data.getData();
}
Log.i(TAG, "Image path: " + targetUri);
if(targetUri != null) {
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
Bitmap photo = getScaledBitmapFromUri(getContext(), targetUri);
imageView.setImageBitmap(photo);
} else {
CommonUtilities.toastShort(getContext(), "Unable to retrieve image, please retake...");
}
}
}
}
public static Bitmap getScaledBitmapFromUri(Context context, Uri uriImageFile) {
Bitmap scaledBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 4;
try {
BitmapFactory.decodeStream(context.getContentResolver()
.openInputStream(uriImageFile), null, options);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
return null;
}
int srcWidth = options.outWidth;
int scale = 1;
while (srcWidth / 2 > 60) {
srcWidth /= 2;
scale *= 2;
}
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = scale;
try {
scaledBitmap = BitmapFactory.decodeStream(context
.getContentResolver().openInputStream(uriImageFile), null, options);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return scaledBitmap;
}