介绍
这篇文章给大家分享的是有关Android怎么获取可拉的目录图片并存入指定文件的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
Android是什么
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国谷歌公司和开放手机联盟领导及开发。
第一步:获取存储的路径我们用<代码>/sdcard/Android/数据/> 代码包名/的路径方便我们测试查看
, String 路径=MyApplication.getContextObject () .getExternalFilesDir (“;”) .toString (); ,File 文件=new 文件(路径),
第二步:根据该文件中存储的路径信息在文件系统上创建一个新的空文件
File finalImageFile =, new 文件(文件,,System.currentTimeMillis (), +,“.jpg"); ,try { ,,finalImageFile.createNewFile (); ,}catch (IOException e), { ,,e.printStackTrace (); 以前,}>第三步:将字节放入文件输出流
FileOutputStream fos =,空; ,try { ,,fos =, new FileOutputStream (finalImageFile); ,}catch (FileNotFoundException e), { ,,e.printStackTrace (); 以前,}>第四步:将图片压缩成图片格式
, BitmapDrawable BitmapDrawable =, (BitmapDrawable) MyApplication.getContextObject () .getResources () .getDrawable (R.drawable.account); 位图,Bitmap =bitmapDrawable.getBitmap (); ,if (bitmap ==, null), { ,,Toast.makeText (MyApplication.getContextObject(),“图片不存在“,Toast.LENGTH_LONG),告诉(); ,才能回报; ,} ,bitmap.compress (Bitmap.CompressFormat.PNG,, 100,, fos)提交; ,try { ,,fos.flush (); ,,fos.close (); ,,Toast.makeText (MyApplication.getContextObject(),“图片保存在:“+,finalImageFile.getAbsolutePath (),, Toast.LENGTH_LONG),告诉(); ,}catch (IOException e), { ,,e.printStackTrace (); 以前,}>完整代码
, String 路径=MyApplication.getContextObject () .getExternalFilesDir (“;”) .toString (); ,File 文件=new 文件(路径); , ,File finalImageFile =, new 文件(文件,,System.currentTimeMillis (), +,“.jpg"); ,try { ,,finalImageFile.createNewFile (); ,}catch (IOException e), { ,,e.printStackTrace (); ,} , ,FileOutputStream fos =,空; ,try { ,,fos =, new FileOutputStream (finalImageFile); ,}catch (FileNotFoundException e), { ,,e.printStackTrace (); ,} , ,BitmapDrawable BitmapDrawable =, (BitmapDrawable) MyApplication.getContextObject () .getResources () .getDrawable (R.drawable.account); 位图,Bitmap =bitmapDrawable.getBitmap (); ,if (bitmap ==, null), { ,,Toast.makeText (MyApplication.getContextObject(),“图片不存在“,Toast.LENGTH_LONG),告诉(); ,才能回报; ,} ,bitmap.compress (Bitmap.CompressFormat.PNG,, 100,, fos)提交; ,try { ,,fos.flush (); ,,fos.close (); ,,Toast.makeText (MyApplication.getContextObject(),“图片保存在:“+,finalImageFile.getAbsolutePath (),, Toast.LENGTH_LONG),告诉(); ,}catch (IOException e), { ,,e.printStackTrace (); ,}感谢各位的阅读!关于“Android怎么获取可拉的目录图片并存入指定文件”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
Android怎么获取可拉的目录图片并存入指定文件