保存BitMap,File到本地
16lz
2021-01-23
保存BitMap到本地
/** * * * @param context * @param mBitmap * @return */public static String saveBitmap(Context context, Bitmap mBitmap) {String savePath = null;File filePic;if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/zplay";} else {savePath = Environment.getDataDirectory().getAbsolutePath()+ "/zplay";}try {filePic = new File(savePath + "/"+ (System.currentTimeMillis() / 1000) + ".jpg");if (!filePic.exists()) {filePic.getParentFile().mkdirs();}FileOutputStream fos = new FileOutputStream(filePic);mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);fos.flush();fos.close();return filePic.getAbsolutePath();} catch (IOException e) {e.printStackTrace();}return "";}
保存File到本地
File file = getFile("/zplay/1.mp4"); try { InputStream inputStream = getAssets().open("1.mp4"); Log.i(TAG, "--file " + file.getAbsolutePath()); if (!file.exists()) { //创建文件夹 //file.mkdirs(); //创建文件 file.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(file); byte[] arr = new byte[1024 * 1024]; int temp = -1; while ((temp = inputStream.read(arr)) != -1) { fileOutputStream.write(arr); fileOutputStream.flush(); Log.i(TAG, "--temp=" + temp); } inputStream.close(); fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); }
private File getFile(String path) { File file = null; String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { //存在sd卡 file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path); } else { file = new File(Environment.getDownloadCacheDirectory().getAbsolutePath() + path); } return file; }
更多相关文章
- android文件操作的实例
- Android 以流的方式读取服务器图片文件
- Android 文件浏览器源码
- Android文件保存和读取
- android Studio导入source文件
- android studio 使用adb 命令传递文件到android 设备的方法
- Android NDK开发使用以及so文件生成和注意事项
- 关于获取Android中文件内容有感
- iOS开发-Android 录制的mp4视频文件在IOS中无法播放问题