gpt4 book ai didi

android - Intent extra 返回 null

转载 作者:行者123 更新时间:2023-11-29 01:39:02 26 4
gpt4 key购买 nike

我从 girdview 适配器类调用 startActivityForResult 事件。 Gridview 适配器类是由 myFragment 类创建的。 intent extras 始终返回 null onActivityResult 事件。你认为错误在哪里?谢谢。

我的 gridview 适配器类;

 public class MyAdapter extends ArrayAdapter<MyInfo> {

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = convertView;

if (view == null) {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(layoutResourceID, null);

}

ImageButton btnImage = (ImageButton) view.findViewById(R.id.btnImage);

@Override
public boolean onLongClick(View v) {

Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

intentCamera.putExtra("Asd", "Asd");

MyFragment.startActivityForResult(intentCamera, 1);

return true;
}
});
}

OnActivityResult 事件的 MyFragment 类

public class MyFragment extends Fragment {

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

ImageButton btn = (ImageButton) getActivity().findViewById(R.id.btnImage);

switch (requestCode) {
case 1:

Bitmap photo = (Bitmap) data.getExtras().get("data");

//photo is OK

Bundle bundle = data.getExtras();

if (bundle.getString("Asd") != null) {

//bundle.getString("Asd") NULL }

if (data.hasExtra("Asd")) {

//data.hasExtra("Asd") false
}


break;

default:
break;
}

}
}

最佳答案

相机应用不会返回您的自定义字符串“Asd”——它不是这样工作的。

您可以期望将照片图像或图像文件返回给您。

如果您只是询问“Asd”,那么这就是您的答案 - 您将无法取回。如果您问为什么没有返回图片,请继续阅读。


(作为先前答案的一部分保留)

如果您想保存相机拍摄的照片,可以按照以下步骤操作:

您需要在 Intent 中将正确的数据传递给相机。根据 image capture Intent 的 Android 开发者文档:

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

... 其中方法 getOutputMediaFileUri() 是您编写的用于创建要保存图像的文件的方法。


有关调用相机应用的更多信息,请参阅以下类似问题:

在那里你会看到一些更多的工作示例。

关于android - Intent extra 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25973051/

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