我正在尝试从相机捕获图像并将结果放入网格视图中。我使用片段如下。我在尝试接收 Intent Extras 时收到 OnActivityResult 上的空指针异常:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
try {
FileOutputStream outStream = new FileOutputStream(PATH_TO_SAVE+"img2.png");
//NULL pointer exception here
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
if (success) {
Log.i("SF", "Image saved with success");
} else {
Log.i("SF", "Image saved with F");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
// 这里开始活动
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.submit_feed_scr2 , container, false);
Button b = (Button) view.findViewById(R.id.btnCapture);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
File(PATH_TO_SAVE)));
startActivityForResult(intent, CAPTURE_ITEM);
}
});
//Adapter
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
items);
GridView gridview = (GridView) view.findViewById(R.id.gridView1);
gridview.setAdapter(adapter);
//Providing Grid with Images
gridview = (GridView) view.findViewById(R.id.gridView1);
gridview.setAdapter(new ImageAdapter(view.getContext(),1));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(this, "" + position, Toast.LENGTH_SHORT).show();
}
});
return view;
}