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);
}
}
x、Android保存图片至相册原生代码
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Andorid保存图片至相册功能实现网上能搜到解决方式普遍是方式一,但是实际测试发现,方式一是有问题的。实现方式一...