package view;
import java.io.ByteArrayOutputStream;
import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import com.example.viewpagetest.R;
public class SecondFragment extends Fragment implements OnClickListener {
Button weixinbutton;
ImageView photo;
AlertDialog.Builder pictureBuilder;
static final int requestcodetake = 1;
static final int requestcodepick = 2;
// String[] items =
// getResources().getStringArray(R.array.select_dialog_items);
String[] items = { "take photo", "pick photo" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment2, container, false);
weixinbutton = (Button) v.findViewById(R.id.button);
photo = (ImageView) v.findViewById(R.id.picture);
weixinbutton.setOnClickListener(this);
photo.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.picture:
AlertDialog.Builder pictureBuilder = new AlertDialog.Builder(
getActivity());
pictureBuilder.setItems(items,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String select_item = items[which];
/*File imageView = new File(Environment.getExternalStorageDirectory(),
"takephoto.jpg");
try {
if(imageView.exists()){
imageView.delete();
}
imageView.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
Uri imageViewUri = Uri.fromFile(imageView);*/
if (select_item == "take photo") {
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, imageViewUri);
startActivityForResult(intent, requestcodetake);
} else if (select_item == "pick photo") {
Intent intent2 = new Intent(Intent.ACTION_PICK);
intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent2,requestcodepick);
}
}
}).show();
break;
default:
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO 自动生成的方法存根
switch (requestCode) {
case requestcodetake:
if(resultCode==Activity.RESULT_OK){
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// convert byte array to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length);
photo.setImageBitmap(bitmap);
}
break;
case requestcodepick:
if (data != null) {
photo.setImageURI(data.getData());
}
default:
break;
}
//super.onActivityResult(requestCode, resultCode, data);
}
}
import java.io.ByteArrayOutputStream;
import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import com.example.viewpagetest.R;
public class SecondFragment extends Fragment implements OnClickListener {
Button weixinbutton;
ImageView photo;
AlertDialog.Builder pictureBuilder;
static final int requestcodetake = 1;
static final int requestcodepick = 2;
// String[] items =
// getResources().getStringArray(R.array.select_dialog_items);
String[] items = { "take photo", "pick photo" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment2, container, false);
weixinbutton = (Button) v.findViewById(R.id.button);
photo = (ImageView) v.findViewById(R.id.picture);
weixinbutton.setOnClickListener(this);
photo.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.picture:
AlertDialog.Builder pictureBuilder = new AlertDialog.Builder(
getActivity());
pictureBuilder.setItems(items,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String select_item = items[which];
/*File imageView = new File(Environment.getExternalStorageDirectory(),
"takephoto.jpg");
try {
if(imageView.exists()){
imageView.delete();
}
imageView.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
Uri imageViewUri = Uri.fromFile(imageView);*/
if (select_item == "take photo") {
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, imageViewUri);
startActivityForResult(intent, requestcodetake);
} else if (select_item == "pick photo") {
Intent intent2 = new Intent(Intent.ACTION_PICK);
intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent2,requestcodepick);
}
}
}).show();
break;
default:
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO 自动生成的方法存根
switch (requestCode) {
case requestcodetake:
if(resultCode==Activity.RESULT_OK){
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// convert byte array to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length);
photo.setImageBitmap(bitmap);
}
break;
case requestcodepick:
if (data != null) {
photo.setImageURI(data.getData());
}
default:
break;
}
//super.onActivityResult(requestCode, resultCode, data);
}
}