feat(system): write to system media for tack photo
This commit is contained in:
@@ -14,6 +14,7 @@ import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
import com.getcapacitor.BridgeActivity;
|
||||
import com.imyeyu.gao.customer.mediasaver.MediaSaverPlugin;
|
||||
import com.imyeyu.gao.customer.shellupdater.ShellUpdaterPlugin;
|
||||
|
||||
import java.util.Locale;
|
||||
@@ -22,6 +23,7 @@ public class MainActivity extends BridgeActivity {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
registerPlugin(ShellUpdaterPlugin.class);
|
||||
registerPlugin(MediaSaverPlugin.class);
|
||||
super.onCreate(savedInstanceState);
|
||||
applyEdgeToEdge();
|
||||
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
package com.imyeyu.gao.customer.mediasaver;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentValues;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Base64;
|
||||
import com.getcapacitor.JSObject;
|
||||
import com.getcapacitor.Plugin;
|
||||
import com.getcapacitor.PluginCall;
|
||||
import com.getcapacitor.PluginMethod;
|
||||
import com.getcapacitor.annotation.CapacitorPlugin;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/// 把网页里拍到的照片/视频写进系统相册
|
||||
///
|
||||
/// 走 MediaStore 的 IS_PENDING 流程,Android 10 起不需要任何存储权限。
|
||||
/// 分块接收是为了避免大视频在 JS 与原生之间一次性复制导致内存翻倍。
|
||||
@CapacitorPlugin(name = "MediaSaver")
|
||||
public class MediaSaverPlugin extends Plugin {
|
||||
|
||||
/// 单个文件上限,防止异常调用把手机存储写满
|
||||
private static final long MAX_BYTES = 512L * 1024 * 1024;
|
||||
|
||||
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
private final Map<String, Session> sessions = new ConcurrentHashMap<>();
|
||||
|
||||
private static class Session {
|
||||
final File temp;
|
||||
final String fileName;
|
||||
final String mimeType;
|
||||
final boolean video;
|
||||
long size;
|
||||
|
||||
Session(File temp, String fileName, String mimeType, boolean video) {
|
||||
this.temp = temp;
|
||||
this.fileName = fileName;
|
||||
this.mimeType = mimeType;
|
||||
this.video = video;
|
||||
}
|
||||
}
|
||||
|
||||
/// 开始一次写入,返回后续分块要用的 id
|
||||
@PluginMethod
|
||||
public void begin(PluginCall call) {
|
||||
String fileName = call.getString("fileName");
|
||||
String mimeType = call.getString("mimeType");
|
||||
String mediaType = call.getString("mediaType", "image");
|
||||
if (fileName == null || !fileName.matches("[\\w.\\-]{1,120}")) {
|
||||
call.reject("文件名不合法");
|
||||
return;
|
||||
}
|
||||
if (mimeType == null || !mimeType.matches("(image|video)/[\\w.+-]+")) {
|
||||
call.reject("媒体类型不合法");
|
||||
return;
|
||||
}
|
||||
File directory = new File(getContext().getCacheDir(), "media-save");
|
||||
if (!directory.exists() && !directory.mkdirs()) {
|
||||
call.reject("无法创建缓存目录");
|
||||
return;
|
||||
}
|
||||
String id = UUID.randomUUID().toString();
|
||||
Session session = new Session(new File(directory, id + ".part"), fileName, mimeType, "video".equals(mediaType));
|
||||
sessions.put(id, session);
|
||||
JSObject result = new JSObject();
|
||||
result.put("id", id);
|
||||
call.resolve(result);
|
||||
}
|
||||
|
||||
/// 追加一块 base64 数据
|
||||
@PluginMethod
|
||||
public void append(PluginCall call) {
|
||||
String id = call.getString("id");
|
||||
String data = call.getString("data");
|
||||
Session session = id == null ? null : sessions.get(id);
|
||||
if (session == null) {
|
||||
call.reject("会话不存在");
|
||||
return;
|
||||
}
|
||||
if (data == null) {
|
||||
call.reject("数据为空");
|
||||
return;
|
||||
}
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
byte[] bytes = Base64.decode(data, Base64.DEFAULT);
|
||||
if (MAX_BYTES < session.size + bytes.length) {
|
||||
throw new IllegalStateException("文件超过大小上限");
|
||||
}
|
||||
try (FileOutputStream output = new FileOutputStream(session.temp, true)) {
|
||||
output.write(bytes);
|
||||
}
|
||||
session.size += bytes.length;
|
||||
call.resolve();
|
||||
} catch (Exception e) {
|
||||
sessions.remove(id);
|
||||
session.temp.delete();
|
||||
call.reject("写入失败", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// 收尾:把临时文件搬进相册,成功后清理临时文件
|
||||
@PluginMethod
|
||||
public void finish(PluginCall call) {
|
||||
String id = call.getString("id");
|
||||
Session session = id == null ? null : sessions.remove(id);
|
||||
if (session == null) {
|
||||
call.reject("会话不存在");
|
||||
return;
|
||||
}
|
||||
executor.execute(() -> {
|
||||
Uri uri = null;
|
||||
try {
|
||||
ContentResolver resolver = getContext().getContentResolver();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(MediaStore.MediaColumns.DISPLAY_NAME, session.fileName);
|
||||
values.put(MediaStore.MediaColumns.MIME_TYPE, session.mimeType);
|
||||
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM + "/GaoCustomer");
|
||||
values.put(MediaStore.MediaColumns.IS_PENDING, 1);
|
||||
Uri collection = session.video
|
||||
? MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
||||
: MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
|
||||
uri = resolver.insert(collection, values);
|
||||
if (uri == null) {
|
||||
throw new IllegalStateException("无法创建相册条目");
|
||||
}
|
||||
try (InputStream input = new FileInputStream(session.temp); OutputStream output = resolver.openOutputStream(uri)) {
|
||||
if (output == null) {
|
||||
throw new IllegalStateException("无法写入相册");
|
||||
}
|
||||
byte[] buffer = new byte[32768];
|
||||
int size;
|
||||
while ((size = input.read(buffer)) != -1) {
|
||||
output.write(buffer, 0, size);
|
||||
}
|
||||
}
|
||||
// 清掉 PENDING,相册 App 才会看到这个文件
|
||||
ContentValues done = new ContentValues();
|
||||
done.put(MediaStore.MediaColumns.IS_PENDING, 0);
|
||||
resolver.update(uri, done, null, null);
|
||||
session.temp.delete();
|
||||
call.resolve();
|
||||
} catch (Exception e) {
|
||||
if (uri != null) {
|
||||
try {
|
||||
getContext().getContentResolver().delete(uri, null, null);
|
||||
} catch (Exception ignored) {
|
||||
// 清理失败不影响主流程
|
||||
}
|
||||
}
|
||||
session.temp.delete();
|
||||
call.reject("保存到相册失败", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// 放弃本次写入
|
||||
@PluginMethod
|
||||
public void cancel(PluginCall call) {
|
||||
String id = call.getString("id");
|
||||
Session session = id == null ? null : sessions.remove(id);
|
||||
if (session != null) {
|
||||
session.temp.delete();
|
||||
}
|
||||
call.resolve();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleOnDestroy() {
|
||||
executor.shutdown();
|
||||
for (Session session : sessions.values()) {
|
||||
session.temp.delete();
|
||||
}
|
||||
sessions.clear();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
ext {
|
||||
minSdkVersion = 24
|
||||
minSdkVersion = 29
|
||||
compileSdkVersion = 36
|
||||
targetSdkVersion = 36
|
||||
androidxActivityVersion = '1.11.0'
|
||||
|
||||
Reference in New Issue
Block a user