take picture and show in fragment

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);
}
}
在 Android 中,可以通过 hide() 和 show() 方法来切换 Fragment。具体的实现步骤如下: 1. 获取 FragmentManager 对象: ```java FragmentManager fragmentManager = getSupportFragmentManager(); ``` 2. 开启一个 FragmentTransaction 事务: ```java FragmentTransaction transaction = fragmentManager.beginTransaction(); ``` 3. 如果要切换到的 Fragment 还没有被添加到 Activity 中,则使用 add() 方法添加到容器中: ```java transaction.add(R.id.container, newFragment); ``` 4. 如果已经添加过,可以使用 show() 方法显示该 Fragment: ```java transaction.show(newFragment); ``` 5. 使用 hide() 方法隐藏当前的 Fragment: ```java transaction.hide(currentFragment); ``` 6. 最后,提交事务: ```java transaction.commit(); ``` 完整的示例代码如下: ```java // 获取 FragmentManager 对象 FragmentManager fragmentManager = getSupportFragmentManager(); // 开启一个 FragmentTransaction 事务 FragmentTransaction transaction = fragmentManager.beginTransaction(); // 如果要切换到的 Fragment 还没有被添加到 Activity 中,则添加到容器中 if (!newFragment.isAdded()) { transaction.add(R.id.container, newFragment); } // 隐藏当前的 Fragment transaction.hide(currentFragment); // 显示要切换到的 Fragment transaction.show(newFragment); // 提交事务 transaction.commit(); ``` 注意,这里的 currentFragment 和 newFragment 都是 Fragment 对象,需要根据具体的情况进行替换。同时,R.id.container 是一个 FrameLayout 容器,用来承载 Fragment
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值