我正在尝试使用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/4QARZ2QAHqy0BQFtKQUCAgbQoTUBAAVo7gbiwAAAABhzdHRzAAAAAAAAAAEAABEDAAAACgAAAChzdHNjAAAAAAAAAAIAAAABAAAAEwAAAAEAAADmAAAABAAAAAEAAEQgc3RzegAAAAAAAAAAAAARAwAAATAAAAAgAAAAIAAAACAAAAAgAABiUAAABAAAA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AvQAAALkAAAA4AAAA4AAAAGYAAAEJAAAAkQAAAIQAAAC1AAAAQwAAAHoAAACSAAAAYQAAAJ4AAAAzAAAAewAAAI4AAABwAAAApgAAADMAAABVAAAAcwAAAFcAAACCAAAAGwAAAGgAAACYAAAAgQAAAHcAAAA5AAAAOQAAAFgAAABqAAAAkQAAAEAAAABuAAAANgAAAGsAAAArAAAAbAAAAKQAAAA8AAAAgwAAABQAAACAAAAAigAAAWwAAABlAAAATwAAAJkAAAAGAAAAjQAAANwAAAFyAAABBAAAABAAAAAmAAAAUQAAAAMAAABSAAAAAwAAAKMAAAAGAAAAxAAAAWwAAAHgAAAAcwAAAEAAAACwAAAAlQAAAE4AAACOAAAAVAAAALgAAABfAAAAqgAAAOEAAAALAAAAowAAAM8AAAJUAAAAwAAAABkAAADpAAAAGQAAAbEAAAL+AAACcAAAANoAAAD8AAAAmAAAALYAAAByAAAAswAAAAIAAAB4AAAADQAAAQUAAAF+AAABeAAAAHkAAAClAAAAlgAAAKoAAABpAAAAzgAAAAIAAACGAAAADQAAAPsAAAGdAAABnwAAAH4AAACnAAAAkwAAAFYAAABUAAAApAAAAAIAAACTAAAAAgAAAM0AAACLAAAAnQAAAIYAAABJAAAAbQ
顺便说一句,
ActivityTransferer.getInstance().getActivity()
返回当前上下文。请您帮助我,我将不胜感激。谢谢!
最佳答案
解决它!
在这种情况下,解决方案是使方法private String getVideoData(Uri uri)
静态且同步(private static synchronized String getVideoData(Uri uri)
)。我认为问题在于代码中使用的某些操作正在使用静态方法或变量。因此,通过使该方法静态和同步,它将在开始操作之前等待其他线程完成。
在这种情况下,InputStream fis = activity.getContentResolver().openInputStream(uri);
可能是导致问题的原因。