短视频平台搭建,相机截取图片位置的相关代码
代码语言:javascript复制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);
}
}
}
以上就是 短视频平台搭建,相机截取图片位置的相关代码,更多内容欢迎关注之后的文章