x、Android保存图片至相册原生代码
16lz
2021-01-26
package com.xxx.NativeCode;import android.app.AlertDialog;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Environment;import android.text.format.Time;import com.unity3d.player.UnityPlayer;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/** * Created by wanshichen on 2017/3/16. */public class NativeCodeHandle { public static void SaveImageToAlbum(String fromPath) { Time t = new Time(); t.setToNow(); final String toPath = Environment.getExternalStorageDirectory() + String.format("/DCIM/Camera/IMG_xxx_%d%d%d_%d%d%d.jpg", t.year, t.month, t.monthDay, t.hour, t.minute, t.second); CopyFileTo(fromPath, toPath); UnityPlayer.currentActivity.runOnUiThread(new Runnable() { @Override public void run() { scanPhotos(toPath, UnityPlayer.currentActivity); AlertDialog.Builder builder = new AlertDialog.Builder(UnityPlayer.currentActivity) .setTitle("提示") .setMessage("保存成功") .setPositiveButton("确定", null); builder.show(); } }); } public static void CopyFileTo(String fromPath, String toPath) { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(fromPath); fos = new FileOutputStream(toPath); byte[] buffer = new byte[1024]; int byteread = 0; while ((byteread = fis.read(buffer)) != -1) { fos.write(buffer, 0, byteread); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void scanPhotos(String filePath, Context context) { Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(Uri.fromFile(new File(filePath))); context.sendBroadcast(intent); }}
更多相关文章
- 大图片文件压缩保存本地,返回本地url
- Android安卓长按ImageView保存图片到相册
- Unity_打包Android提示找不到jdk解决方案
- android app 捕获全局异常,保存成文件
- android练习一之保存用户名
- Android_rom存储,sp存储,sdcard存储
- unity3d 调用 android sdk 接入
- 保存数据到手机内存代码优化(QQ登录保存密码)
- Android(安卓)电视退出程序Animation