麦子学院Android开发教程拍照保存系统相册不显示照片问题.docx_第1页
麦子学院Android开发教程拍照保存系统相册不显示照片问题.docx_第2页
麦子学院Android开发教程拍照保存系统相册不显示照片问题.docx_第3页
麦子学院Android开发教程拍照保存系统相册不显示照片问题.docx_第4页
麦子学院Android开发教程拍照保存系统相册不显示照片问题.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

IT在线教育平台麦子学院现在几乎所有app应用都可以调用手机的照相功能了,但我在开始时碰到一个问题就是拍照之后在系统相册中找不到我拍照的照片怎么办?下面我来给各位同学一并分享一下。 系统已经有的东西,如果我们没有新的需求的话,直接调用是最直接的。下面讲讲调用系统相机拍照并保存图片和如何调用系统相册的方法。首先看看调用系统相机的核心方法: 代码如下Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(camera, CAMERA);相机返回的数据通过下面的回调方法取得,并处理: 代码如下Override protected void onActivityResult(int requestCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data); if(requestCode = CAMERA & resultCode = Activity.RESULT_OK & null != data) String sdState=Environment.getExternalStorageState(); if(!sdState.equals(Environment.MEDIA_MOUNTED) GameLog.log(Tag, sd card unmount); return; new DateFormat(); String name= DateFormat.format(yyyyMMdd_hhmmss, Calendar.getInstance(Locale.CHINA)+.jpg; Bundle bundle = data.getExtras(); /获取相机返回的数据,并转换为图片格式 Bitmap bitmap = (Bitmap)bundle.get(data); FileOutputStream fout = null; File file = new File(/sdcard/pintu/); file.mkdirs(); String filename=file.getPath()+name; try fout = new FileOutputStream(filename); press(Bitmap.CompressFormat.JPEG, 100, fout); catch (FileNotFoundException e) e.printStackTrace(); finally try fout.flush(); fout.close(); catch (IOException e) e.printStackTrace(); /显示图片 下面是调用系统相册并取得照片的方法: 代码如下Intent picture = new Intent(Intent.ACTION_PICK,vider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivityForResult(picture, PICTURE);下面是相应的回调方法: 代码如下Override protected void onActivityResult(int requestCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data); if(requestCode = CAMERA & resultCode = Activity.RESULT_OK & null != data) Uri selectedImage = data.getData(); String filePathColumns=MediaStore.Images.Media.DATA; Cursor c = this.getContentResolver().query(selectedImage, filePathColumns, null,null, null); c.moveToFirst(); int columnIndex = c.getColumnIndex(filePathColumns0); String picturePath= c.getString(columnIndex); c.close(); /获取图片并显示 这样就完成了系统调用,很简单,但是有些朋友会碰到照片拍好了,在手机相册中发现照片不显示呀。解决Android拍照保存在系统相册不显示的问题MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, , );通过上面的那句代码就能插入到系统图库,这时候有一个问题,就是我们不能指定插入照片的名字,而是系统给了我们一个当前时间的毫秒数为名字,有一个问题郁闷了很久,我还是先把insertImage的源码贴出来吧 代码如下 /* * Insert an image and create a thumbnail for it. * * param cr The content resolver to use * param source The stream to use for the image * param title The name of the image * param description The description of the image * return The URL to the newly created image, or null if the image failed to be stored * for any reason. */ public static final String insertImage(ContentResolver cr, Bitmap source, String title, String description) ContentValues values = new ContentValues(); values.put(Images.Media.TITLE, title); values.put(Images.Media.DESCRIPTION, description); values.put(Images.Media.MIME_TYPE, image/jpeg); Uri url = null; String stringUrl = null; /* value to be returned */ try url = cr.insert(EXTERNAL_CONTENT_URI, values); if (source != null) OutputStream imageOut = cr.openOutputStream(url); try press(Bitmap.CompressFormat.JPEG, 50, imageOut); finally imageOut.close(); long id = ContentUris.parseId(url); / Wait until MINI_KIND thumbnail is generated. Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MINI_KIND, null); / This is for backward compatibility. Bitmap microThumb = StoreThumbnail(cr, miniThumb, id, 50F, 50F, Images.Thumbnails.MICRO_KIND); else Log.e(TAG, Failed to create thumbnail, removing original); cr.delete(url, null, null); url = null; catch (Exception e) Log.e(TAG, Failed to insert image, e); if (url != null) cr.delete(url, null, null); url = null; if (url != null) stringUrl = url.toString(); return stringUrl; 上面方法里面有一个title,我刚以为是可以设置图片的名字,设置一下,原来不是,郁闷,哪位高手知道title这个字段是干嘛的,告诉下小弟,不胜感激!当然Android还提供了一个插入系统相册的方法,可以指定保存图片的名字,我把源码贴出来吧 代码如下 /* * Insert an image and create a thumbnail for it. * * param cr The content resolver to use * param imagePath The path to the image to insert * param name The name of the image * param description The description of the image * return The URL to the newly created image * throws FileNotFoundException */ public static final String insertImage(ContentResolver cr, String imagePath, String name, String description) throws FileNotFoundException / Check if file exists with a FileInputStream / FileInputStream stream = new FileInputStream(imagePath); try Bitmap bm = BitmapFactory.decodeFile(imagePath); String ret = insertImage(cr, bm, name, description); bm.recycle(); return ret; finally try stream.close(); catch (IOException e) 啊啊,贴完源码我才发现,这个方法调用了第一个方法,这个name就是上面方法的title,晕死,这下更加郁闷了,反正我设置title无效果,求高手为小弟解答,先不管了,我们继续往下说上面那段代码插入到系统相册之后还需要发条广播 代码如下 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(file:/+ Environment.getExternalStorageDirectory(); 上面那条广播是扫描整个sd卡的广播,如果你sd卡里面东西很多会扫描很久,在扫描当中我们是不能访问sd卡,所以这样子用户体现很不好,用过微信的朋友都知道,微信保存图片到系统相册并没有扫描整个SD卡,所以我们用到下面的方法 代码如下Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File(/sdcard/image.jpg); intent.setData(uri); mContext.sendBroadcast(intent); 或者用MediaScannerConnection 代码如下 final MediaScannerConnection msc = new MediaScannerConnection(mContext, new MediaScannerConnectionClient() public void onMediaScannerConnected() msc.scanFile(/sdcard/image.jpg, image/jpeg); public void onScanCompleted(String path, Uri uri) Log.v(TAG, scan completed); msc.disconnect(); ); 也行你会问我,怎么获取到我们刚刚插入的图片的路径?

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论