保存BitMap,File到本地
16lz
2021-12-04
保存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文件保存和读取
- Android(安卓)Shareperferences使用
- Android(安卓)Activity从创建到显示流程
- Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程
- Android流媒体播放器