实例介绍
【实例简介】其中也包含了 将图片保存至 sd卡功能
【实例截图】
【核心代码】
public class MainActivity extends Activity{ private static final int PHOTO_WITH_DATA = 18; //从SD卡中得到图片 private static final int PHOTO_WITH_CAMERA = 37;// 拍摄照片 private Button btn_open; private ImageView iv_temp; private Dialog dialog_pic; private String imgPath = ""; private String imgName = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv_temp = (ImageView) findViewById(R.id.imageView); iv_temp.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { openPictureDialog(); } }); btn_open = (Button) findViewById(R.id.btn_open); btn_open.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { openPictureSelectDialog(); } }); } /**打开对话框**/ private void openPictureSelectDialog() { //自定义Context,添加主题 Context dialogContext = new ContextThemeWrapper(MainActivity.this, android.R.style.Theme_Light); String[] choiceItems= new String[2]; choiceItems[0] = "相机拍摄"; //拍照 choiceItems[1] = "本地相册"; //从相册中选择 ListAdapter adapter = new ArrayAdapter<String>(dialogContext, android.R.layout.simple_list_item_1,choiceItems); //对话框建立在刚才定义好的上下文上 AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext); builder.setTitle("添加图片"); builder.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: //相机 doTakePhoto(); break; case 1: //从图库相册中选取 doPickPhotoFromGallery(); break; } dialog.dismiss(); } }); builder.create().show(); } /**打开图片查看对话框**/ private void openPictureDialog() { dialog_pic = new Dialog(MainActivity.this,R.style.simple_dialog); LayoutInflater inflater = getLayoutInflater(); View view = inflater.from(MainActivity.this).inflate(R.layout.dialog_picture, null); ImageView imgView = (ImageView) view.findViewById(R.id.img_weibo_img); Button btnBig = (Button) view.findViewById(R.id.btn_big); btnBig.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,ImgDisplayActivity.class); imgPath = getApplicationContext().getFilesDir() "/" imgName; intent.putExtra("imgUrl", imgPath); //将图片的路径传递过去 startActivity(intent); } }); dialog_pic.setContentView(view); dialog_pic.show(); displayForDlg(imgView,imgPath,btnBig); //显示内容到dialog中 } private void displayForDlg(ImageView imgView, String imgPath2,Button btnBig) { imgView.setVisibility(View.VISIBLE); btnBig.setVisibility(View.VISIBLE); imgPath = getApplicationContext().getFilesDir() "/" imgName; System.out.println("图片文件路径----------》" imgPath); if(!imgPath.equals("")) { Bitmap tempBitmap = BitmapFactory.decodeFile(imgPath); imgView.setImageBitmap(tempBitmap);//显示图片 } } /**You will receive this call immediately before onResume() when your activity is re-starting.**/ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == RESULT_OK) { //返回成功 switch (requestCode) { case PHOTO_WITH_CAMERA: {//拍照获取图片 String status = Environment.getExternalStorageState(); if(status.equals(Environment.MEDIA_MOUNTED)) { //是否有SD卡 Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() "/image.jpg"); imgName = createPhotoFileName(); //写一个方法将此文件保存到本应用下面啦 savePicture(imgName,bitmap); if (bitmap != null) { //为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存 Bitmap smallBitmap = ImageTools.zoomBitmap(bitmap, bitmap.getWidth() / 5, bitmap.getHeight() / 5); iv_temp.setImageBitmap(smallBitmap); } Toast.makeText(MainActivity.this, "已保存本应用的files文件夹下", Toast.LENGTH_LONG).show(); }else { Toast.makeText(MainActivity.this, "没有SD卡", Toast.LENGTH_LONG).show(); } break; } case PHOTO_WITH_DATA: {//从图库中选择图片 ContentResolver resolver = getContentResolver(); //照片的原始资源地址 Uri originalUri = data.getData(); //System.out.println(originalUri.toString()); //" content://media/external/images/media/15838 " // //将原始路径转换成图片的路径 // String selectedImagePath = uri2filePath(originalUri); // System.out.println(selectedImagePath); //" /mnt/sdcard/DCIM/Camera/IMG_20130603_185143.jpg " try { //使用ContentProvider通过URI获取原始图片 Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, originalUri); imgName = createPhotoFileName(); //写一个方法将此文件保存到本应用下面啦 savePicture(imgName,photo); if (photo != null) { //为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存 Bitmap smallBitmap = ImageTools.zoomBitmap(photo, photo.getWidth() / 5, photo.getHeight() / 5); iv_temp.setImageBitmap(smallBitmap); } // iv_temp.setImageURI(originalUri); //在界面上显示图片 Toast.makeText(MainActivity.this, "已保存本应用的files文件夹下", Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } break; } } } super.onActivityResult(requestCode, resultCode, data); } /**从相册获取图片**/ private void doPickPhotoFromGallery() { Intent intent = new Intent(); intent.setType("image/*"); // 开启Pictures画面Type设定为image intent.setAction(Intent.ACTION_GET_CONTENT); //使用Intent.ACTION_GET_CONTENT这个Action startActivityForResult(intent, PHOTO_WITH_DATA); //取得相片后返回到本画面 } /**拍照获取相片**/ private void doTakePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //调用系统相机 Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"image.jpg")); //指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //直接使用,没有缩小 startActivityForResult(intent, PHOTO_WITH_CAMERA); //用户点击了从相机获取 } /**创建图片不同的文件名**/ private String createPhotoFileName() { String fileName = ""; Date date = new Date(System.currentTimeMillis()); //系统当前时间 SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss"); fileName = dateFormat.format(date) ".jpg"; return fileName; } /**获取文件路径**/ public String uri2filePath(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); return path; } /**保存图片到本应用下**/ private void savePicture(String fileName,Bitmap bitmap) { FileOutputStream fos =null; try {//直接写入名称即可,没有会被自动创建;私有:只有本应用才能访问,重新内容写入会被覆盖 fos = MainActivity.this.openFileOutput(fileName, Context.MODE_PRIVATE); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);// 把图片写入指定文件夹中 } catch (Exception e) { e.printStackTrace(); } finally { try { if(null != fos) { fos.close(); fos = null; } }catch(Exception e) { e.printStackTrace(); } } } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)