短视频平台搭建,相机截取图片位置-阿里云开发者社区

开发者社区> 云原生> 正文

短视频平台搭建,相机截取图片位置

简介: 短视频平台搭建,相机截取图片位置

短视频平台搭建,相机截取图片位置的相关代码

package com.example.liany;
import java.io.File;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

public class FragmentB extends Fragment{
    private ImageView imageview;
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = View.inflate(getActivity(), R.layout.fragment2, null);
        
        imageview = (ImageView) inflate.findViewById(R.id.imageview);
        
        imageview.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());
                builder.setItems(new String[]{"相册","拍照"}, new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                        //从相册选取
                        case 0:
                            Intent intent = new Intent(Intent.ACTION_PICK);
                            intent.setType("image/*");
                            startActivityForResult(intent, 1000);
                            break;
                        //拍照
                        case 1:
                            Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            intent2.putExtra(MediaStore.EXTRA_OUTPUT,
                                    Uri.fromFile(new File(Environment
                                            .getExternalStorageDirectory()+"/tianfuzhen.png")));
                            startActivityForResult(intent2, 2000);
                            break;
                         default:
                            break;
                        }
                        
                    }
                });
                builder.show();
            }
        });
        return inflate;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        
        if(requestCode==1000 && resultCode==getActivity().RESULT_OK){
            Uri uri = data.getData();
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");
            
            intent.putExtra("crop", true);
            intent.putExtra("aspactX", 1);
            intent.putExtra("aspactY", 1);
            intent.putExtra("outputX", 200);
            intent.putExtra("outputY", 200);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 1001);
        }
        if(requestCode==1001 && resultCode==getActivity().RESULT_OK){
            Bitmap bitmap = data.getParcelableExtra("data");
            imageview.setImageBitmap(bitmap);
        }
        //相机返回值
        if(requestCode==2000 && resultCode==getActivity().RESULT_OK){
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(
                    Uri.fromFile(new File(Environment
                            .getExternalStorageDirectory()+"/tianfuzhen.png")),"image/*");
            
            intent.putExtra("crop", true);
            intent.putExtra("aspactX", 1);
            intent.putExtra("aspactY", 1);
            intent.putExtra("outputX", 200);
            intent.putExtra("outputY", 200);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 2001);
        }
        if(requestCode==2001 && resultCode==getActivity().RESULT_OK){
            Bitmap bitmap = data.getParcelableExtra("data");
            imageview.setImageBitmap(bitmap);
        }
    }
}

以上就是 短视频平台搭建,相机截取图片位置的相关代码,更多内容欢迎关注之后的文章

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云原生
使用钉钉扫一扫加入圈子
+ 订阅

云原生时代,是开发者最好的时代

其他文章