gpt4 book ai didi

java - 使用 openFileInput(Uri) 和多线程打开多个文件

转载 作者:行者123 更新时间:2023-12-01 18:45:33 24 4
gpt4 key购买 nike

我正在尝试使用 context.getContentResolver().openInputStream(Uri) 打开多个线程的多个文件但似乎在读取时,所有线程都从同一个文件读取。我尝试过使用方法 synchronized但没有用。这是代码:

private String getVideoData(Uri uri) {
String res = null;
MyActivity activity = ActivityTransferer.getInstance().getActivity();

try {
InputStream fis = activity.getContentResolver().openInputStream(uri);

if (fis != null) {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();

byte[] bytes = new byte[4096];

int read = fis.read(bytes);

while (read != -1) {
byteOutputStream.write(bytes);
read = fis.read(bytes);
}

byte[] data = byteOutputStream.toByteArray();

res = java.util.Base64.getEncoder().encodeToString(data);

fis.close();
byteOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}

return res;
}

调用该方法的代码为:

private EncryptedFile encryptUri(Uri uri) {
EncryptedFile res = null;
MyActivity activity = ActivityTransferer.getInstance().getActivity();

String type = activity.getContentResolver().getType(uri);

if (type != null) {
String data;
System.out.println("Path: " + uri.getPath());
System.out.println("Type: " + type);
if (type.startsWith("video")) {

data = getVideoData(uri);
res = new EncryptedVideo();
System.out.println("Data: " + data);
res.encrypt(data, password);

}
else {

data = getImageData(uri);
res = new EncryptedPicture();
res.encrypt(data, password);

}
}

return res;
}

调用自:

while (!toEncrypt.isEmpty()) {
file = toEncrypt.poll();

encrypted = encryptUri(file);

result.add(encrypted);
}

打印 byte[] data = byteOutputStream.toByteArray(); 的前 100 个字节我得到:

I/System.out: 0-0-0-24-102-116-121-112-109-112-52-50-0-0-0-0-109-112-52-50-105-115-111-109-0-0-0-24-98-101-97-109-1-0-0-0-1-0-0-0-0-0-0-0-2-0-0-0-0-0--126-6-109-111-111-118-0-0-0-108-109-118-104-100-0-0-0-0-0-0-0-0-0-0-0-0-0-0-2-88-0-0--86-30-0-1-0-0-1-0-0-0-0-0-0-0-0-0-0-0-

在其他线程上:

I/System.out: 0-0-0-24-102-116-121-112-109-112-52-50-0-0-0-0-109-112-52-50-105-115-111-109-0-0-0-24-98-101-97-109-1-0-0-0-1-0-0-0-0-0-0-0-2-0-0-0-0-0--126-6-109-111-111-118-0-0-0-108-109-118-104-100-0-0-0-0-0-0-0-0-0-0-0-0-0-0-2-88-0-0--86-30-0-1-0-0-1-0-0-0-0-0-0-0-0-0-0-0-

打印方式:

for (int i = 0; i < 100; i++)
System.out.print(data[i] + "-");

使用 Base64 编码(显然两者也相同):

AAAAGGZ0eXBtcDQyAAAAAG1wNDJpc29tAAAAGGJlYW0BAAAAAQAAAAAAAAACAAAAAACCBm1vb3YAAABsbXZoZAAAAAAAAAAAAAAAAAAAAlgAAKoeAAEAAAEAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAEnZdHJhawAAAFx0a2hkAAAABwAAAAAAAAAAAAAAAQAAAAAAAKoeAAAAAAAAAAAAAAAAAQAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAKAAAABYAAAAABJdW1kaWEAAAAgbWRoZAAAAAAAAAAAAAAAAAAAAlgAAKoeVcQAAAAAACJoZGxyAAAAAAAAAAB2aWRlAAAAAAAAAAAAAAAAAAAAAEkrbWluZgAAABR2bWhkAAAAAAAAAAAAAAAAAAAAJGRpbmYAAAAcZHJlZgAAAAAAAAABAAAADHVybCAAAAABAABI63N0YmwAAACPc3RzZAAAAAAAAAABAAAAf2F2YzEAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAACgAFgAEgAAABIAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY//8AAAApYXZjQwFkAB7/4QARZ2QAHqy0BQFtKQUCAgbQoTUBAAVo7gbiwAAAABhzdHRzAAAAAAAAAAEAABEDAAAACgAAAChzdHNjAAAAAA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AACwAAAAPwAAAL4AAACnAAAAzwAAAI8AAABWAAAAZQAAAGQAAABVAAAAlAAAAHkAAABvAAAAkQAAADIAAADZAAAAVgAAAScAAABfAAAAvQAAALkAAAA4AAAA4AAAAGYAAAEJAAAAkQAAAIQAAAC1AAAAQwAAAHoAAACSAAAAYQAAAJ4AAAAzAAAAewAAAI4AAABwAAAApgAAADMAAABVAAAAcwAAAFcAAACCAAAAGwAAAGgAAACYAAAAgQAAAHcAAAA5AAAAOQAAAFgAAABqAAAAkQAAAEAAAABuAAAANgAAAGsAAAArAAAAbAAAAKQAAAA8AAAAgwAAABQAAACAAAAAigAAAWwAAABlAAAATwAAAJkAAAAGAAAAjQAAANwAAAFyAAABBAAAABAAAAAmAAAAUQAAAAMAAABSAAAAAwAAAKMAAAAGAAAAxAAAAWwAAAHgAAAAcwAAAEAAAACwAAAAlQAAAE4AAACOAAAAVAAAALgAAABfAAAAqgAAAOEAAAALAAAAowAAAM8AAAJUAAAAwAAAABkAAADpAAAAGQAAAbEAAAL+AAACcAAAANoAAAD8AAAAmAAAALYAAAByAAAAswAAAAIAAAB4AAAADQAAAQUAAAF+AAABeAAAAHkAAAClAAAAlgAAAKoAAABpAAAAzgAAAAIAAACGAAAADQAAAPsAAAGdAAABnwAAAH4AAACnAAAAkwAAAFYAAABUAAAApAAAAAIAAACTAAAAAgAAAM0AAACLAAAAnQAAAIYAAABJAAAAbQ

顺便说一下,ActivityTransferer.getInstance().getActivity()返回当前上下文。

如果您能帮助我,我将非常感激。谢谢!

最佳答案

已修复!

这种情况下的解决方案是使方法 private String getVideoData(Uri uri) 静态并同步 (private static synchronized String getVideoData(Uri uri))。我认为问题在于代码中使用的某些操作使用静态方法或变量。因此,通过使方法静态并同步,它将等待其他线程完成后再开始操作。

在这种情况下,InputStream fis = Activity.getContentResolver().openInputStream(uri); 可能是导致问题的原因。

关于java - 使用 openFileInput(Uri) 和多线程打开多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59848825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com