80,352
社区成员
发帖
与我相关
我的任务
分享
public class MainActivity extends BaseActivity implements SetMyInfoAvatar{
... ...
private static final int REQ_CAMERA = 0x01;
private static final int REQ_CUT = 0x04;
private File picName;
private File cropPicName;
... ...
@Override
public Bitmap getAvatar(){
//拍照,并通过onActivityResult函数调用裁剪图片的方法
applyPermissionForCamera();
//返回裁剪完成后图片的Bitmap对象
return BitmapFactory.decodeFile(cropPicName.getAbsolutePath());
//这句话报NullPointerException异常
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK)
{
switch (requestCode){
case REQ_CAMERA:
//拍照完成,开始裁剪
cropPic(picName);
//裁剪完成,保存在cropPicName中
break;
case REQ_CUT:
break;
}
}
}
}
public interface SetMyInfoAvatar {
public Bitmap getAvatar();
}
public class MyFragment extends Fragment{
private SetMyInfoAvatar setAvatar;
... ...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.layout_myinfofragment,container,false);
ImageView mAvatar = view.findViewById(R.id.Avatar);
mAvatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAvatar.setImageBitmap(setAvatar.getAvatar());
}
});
return view;
}
@Override
public void onAttach(Context context){
super.onAttach(context);
setAvatar = (SetMyInfoAvatar)((Activity)context);
}
... ...
}
public interface SetMyInfoAvatar {
public void setAvatar(Bitmap bitmap);
}
fragment在setAvatar中设置图片。
ps:在fragment中的拍照和裁剪就行了,不必使用Activity的方法再传递到fragment中,多此一举吧。