zoukankan      html  css  js  c++  java
  • android 读,写图片sd网卡资源

    <pre name="code" class="html"><!--<span style="font-family: Arial, Helvetica, sans-serif;">在AndroidMainfest.xml文件里加入</span><span style="font-family: Arial, Helvetica, sans-serif;">--></span>
    <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></uses-permission> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission> <uses-permission android:name="android.permission.RESTART_PACKAGES" ></uses-permission>
    <!--<span style="font-family: Arial, Helvetica, sans-serif;">注意:在<application...></application>前加入</span><span style="font-family: Arial, Helvetica, sans-serif;">--></span>
    
    
    /**
         * Save Bitmap to a file.保存图片到SD卡。
         * 
         * @param bitmap
         * @param file
         * @return error message if the saving is failed. null if the saving is
         *         successful.
         * @throws IOException
         */
        public static void saveBitmapToFile(Bitmap bitmap, String _file)
                throws IOException {//_file = <span style="font-family: Arial, Helvetica, sans-serif;">getSDPath()+"</span><span style="font-family: Arial, Helvetica, sans-serif;">/xx自己定义目录</span><span style="font-family: Arial, Helvetica, sans-serif;">/hot.png</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span>
            BufferedOutputStream os = null;
            try {
                File file = new File(_file);
                // String _filePath_file.replace(File.separatorChar +
                // file.getName(), "");
                int end = _file.lastIndexOf(File.separator);
                String _filePath = _file.substring(0, end);
                File filePath = new File(_filePath);
                if (!filePath.exists()) {
                    filePath.mkdirs();
                }
                file.createNewFile();
                os = new BufferedOutputStream(new FileOutputStream(file));
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        Log.e(TAG_ERROR, e.getMessage(), e);
                    }
                }
            }
        }
    <pre name="code" class="java">//读取
    public class MainAct extends Activity {
            /** Called when the activity is first created. */
            private ImageView img;
            //SD图片路径
            private String filepath = <span style="font-family: Arial, Helvetica, sans-serif;">getSDPath()+"</span><span style="font-family: Arial, Helvetica, sans-serif;">/xx自己定义目录</span><span style="font-family: Arial, Helvetica, sans-serif;">/hot.png</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span>
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                    img = (ImageView) findViewById(R.id.img);
                    File file = new File(filepath);
                    if (file.exists()) {
                            Bitmap bm = BitmapFactory.decodeFile(filepath);
                            //将图片显示到ImageView中
                            img.setImageBitmap(bm);
                    }
            }
    }
    /**
    	 * 获取SDK路径
    	 * @return
    	 */
    	public static String getSDPath(){ 
    	       File sdDir = null; 
    	       boolean sdCardExist = Environment.getExternalStorageState()   
    	                           .equals(android.os.Environment.MEDIA_MOUNTED);   //推断sd卡是否存在 
    	       if   (sdCardExist)   
    	       {                               
    	         sdDir = Environment.getExternalStorageDirectory();//获取与文件夹 
    	      }   
    	       return sdDir.toString(); 
    	       
    	}


    
       
    
  • 相关阅读:
    HTML CSS整理笔记
    2020软件工程最后一次作业
    form表单的基本用法
    图片预加载和懒加载(2)——懒加载
    ES6——promise基础
    图片预加载和懒加载(2)——预加载
    图片预加载和懒加载(1)
    js时间——转换为我们需要的格式
    原生js瀑布流
    富文本——小程序中使用特殊符号及标签
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5043199.html
Copyright © 2011-2022 走看看