package com.bwei.yangliu20180924.ui.fragment; import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.Button; import android.widget.ImageView; import android.widget.PopupWindow; import com.bwei.yangliu20180924.R; import java.io.File; import java.io.IOException; import static android.app.Activity.RESULT_OK; /** * Created by 杨柳 on 2018/9/24. */ public class MyFragment extends Fragment{ private ImageView img_touxiang; private Button btn_xiangji; private Button btn_xiangce; private Button btn_cancle; private Uri imageUri; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); img_touxiang = view.findViewById(R.id.img_touxiang); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); img_touxiang.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { getpop(); } }); } private void getpop() { View popView = LayoutInflater.from(getContext()).inflate(R.layout.pop, null); btn_xiangji = popView.findViewById(R.id.btn_xiangji); btn_xiangce = popView.findViewById(R.id.btn_xiangce); btn_cancle = popView.findViewById(R.id.btn_cancle); PopupWindow popupWindow = new PopupWindow(popView, AbsListView.LayoutParams.MATCH_PARENT, 300, true); // 设置PopupWindow的背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置PopupWindow是否能响应外部点击事件 popupWindow.setOutsideTouchable(true); // 设置PopupWindow是否能响应点击事件 popupWindow.setTouchable(true); popupWindow.showAtLocation(getView(),Gravity.BOTTOM,0,0); btn_xiangji.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { File outputImage = new File(Environment.getExternalStorageDirectory(), "tempImage" + ".jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, 1); } }); //相册选择 btn_xiangce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, 1); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if(data != null) { Uri uri = data.getData(); imageUri = uri; } img_touxiang.setImageURI(imageUri); //imgMeMyinfoUser.setImageBitmap(bitmap); } } }
拍照
最新推荐文章于 2024-02-26 21:30:24 发布