1. 程式人生 > >相機相簿上傳頭像

相機相簿上傳頭像

public class FragmentFour extends Fragment {

    @BindView(R.id.image_view)
    ImageView imageView;
    Unbinder unbinder;
    @BindView(R.id.llusericonstring)
    LinearLayout llusericonstring;
    private PopupWindow window;
    private String path = Environment.getDownloadCacheDirectory() + "/pics.jpg";
    private Button button_xiangji;
    private Button button_xiangce;
    private Button button_finish;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentfour, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        View contentView = View.inflate(getActivity(), R.layout.pop_layout, null);
        window = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        window.setTouchable(true);
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new BitmapDrawable());

        button_xiangji = contentView.findViewById(R.id.button_xiangji);
        button_xiangce = contentView.findViewById(R.id.button_xiangce);
        button_finish = contentView.findViewById(R.id.button_finish);
        button_xiangji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phone(view);
            }
        });

        button_xiangce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                album(view);
            }
        });

        button_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 999 && resultCode == getActivity().RESULT_OK) {
            Uri uri = data.getData();
            crop(uri);
        } else if (requestCode == 888 && resultCode == getActivity().RESULT_OK) {
            Uri uri = Uri.fromFile(new File(path));
            crop(uri);
        } else if (requestCode == 1000 && resultCode == getActivity().RESULT_OK) {
            Bitmap bitmap = data.getParcelableExtra("data");

            imageView.setImageBitmap(bitmap);
        }
    }

    private void crop(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", true);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 1000);
    }

    private void phone(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
        startActivityForResult(intent, 888);
        window.dismiss();
    }

    private void album(View view) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, 999);
        window.dismiss();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.image_view)
    public void onViewClicked() {

        window.showAtLocation(llusericonstring, Gravity.BOTTOM, 0, 0);

    }
}
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:id="@+id/llusericonstring"
     >

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="個人中心"
         android:layout_gravity="center"
         />

     <ImageView
         android:layout_width="100dp"
         android:layout_height="100dp"
         android:id="@+id/image_view"
         android:src="@drawable/navtab_home"
         />

 </LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/button_xiangji"
        android:text="相機"
        />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/button_xiangce"
        android:text="相簿"
        />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/button_finish"
        android:text="取消"
        />

</LinearLayout>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />