gpt4 book ai didi

Android 相机捕获 Activity 返回空 Uri

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:16 24 4
gpt4 key购买 nike

此代码以前适用于三星,但现在我在 Android 2.3.6 上使用 Nexus One,只要我拍照并单击“确定”或从图库中选择照片,它就会崩溃。 Stacktrace 在 Uri 上显示空指针异常。
我激活相机的代码如下:

public void activateCamera(View view){      
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// start the image capture Intent
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if ((requestCode == CHOOSE_IMAGE_ACTIVITY_REQUEST_CODE || requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
&& resultCode == RESULT_OK && null != data) {

selectedImage = data.getData();

String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);

cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

String picturePath = cursor.getString(columnIndex);

cursor.close();

Bitmap bits = null;

BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = inSampleSize;

try {
bits = BitmapFactory.decodeStream(new FileInputStream(picturePath),null,options);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

知道可能是什么问题吗?谢谢!

最佳答案

你必须告诉相机,将图片保存到哪里并自己记住uri:

private Uri mMakePhotoUri;

private File createImageFile() {
// return a File object for your image.
}

private void makePhoto() {
try {
File f = createImageFile();
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMakePhotoUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, REQUEST_MAKE_PHOTO);
} catch (IOException e) {
Log.e(TAG, "IO error", e);
Toast.makeText(getActivity(), R.string.error_writing_image, Toast.LENGTH_LONG).show();
}
}

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_MAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
// do something with mMakePhotoUri
}
return;
default: // do nothing
super.onActivityResult(requestCode, resultCode, data);
}
}

您应该使用 onCreate()onSaveInstanceState()mMakePhotoUri 的值保存在实例状态上。

关于Android 相机捕获 Activity 返回空 Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18501439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com