Я новичок в AsyncTask, поэтому заранее извиняюсь, если моя проблема глупая. Короче говоря, у меня есть метод, который обрабатывает некоторые файлы, и я хочу запустить его в фоновом режиме. Мой класс ниже, и проблема в том, что метод работает нормально, когда вызывается напрямую, но абсолютно ничего не происходит, когда я пытаюсь вызвать его через doInBackground.
Это работает: AttachFilesFromFolder.attach(files);
И это не так: new AttachFilesFromFolder().execute(files);
Рассматриваемый класс:
public class AttachFilesFromFolder extends AsyncTask<File[], Void, Void> {
@Override
protected Void doInBackground(File[]... files) {
try {
attach(files[0]);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
public static void attach(File[] files) throws InterruptedException {
for (File file : files) {
log("For loop started.");
File targetLocation = new File(Environment.getDataDirectory() + "/data/org.p4.epo.android/" + file.getName());
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(targetLocation);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
buf = null;
in.close();
in = null;
out.close();
out = null;
log("Copied file " + targetLocation.toString() + " successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
SdkManager.sharedInstance().addAttachment(targetLocation.toString(), file.getName(), AttachMode.asNewPage, 1, false);
log("Attached " + file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
});
log("Thread T1 started.");
t1.start();
t1.join();
log("Thread T2 started.");
t2.start();
t2.join();
}
}
}