我使用@ 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 =(File)savedInstanceState.getSerializable(EXTRA_FILENAME);)
答案 0 :(得分:1)
尝试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;
}
答案 1 :(得分:0)
那么我做错了什么?
您设置EXTRA_OUTPUT
,然后忽略它。您的照片可能位于您向EXTRA_OUTPUT
提供的位置。我说"可能"因为您使用ACTION_IMAGE_CAPTURE
要求随机的第三方应用来拍照,而此类应用有时会出现错误。
此活动来自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
附加中使用的那样。