我正在开发一个android应用程序,可以从后台将特定文件夹的图像一一上传到服务器,而不中断UI。我实现的是来自launcher活动的intentService调用,该活动具有读取READ_EXTERNAL_STORAGE的运行时权限。我需要上传文件夹中的所有文件,还需要上传创建或移动到该文件夹的新文件。为此,我创建了一个文件观察者类,我认为它不能正常工作。
public class DirectoryFileObserver extends FileObserver {
String aboslutePath = "";
public DirectoryFileObserver(String path) {
super(path, FileObserver.ALL_EVENTS);
aboslutePath = path;
Log.i("watch path", path);
}
@Override
public void onEvent(int event, String path) {
Log.i("FileObserver++", "File Created:" + path);
File file = new File(aboslutePath + "/" + path);
List<Skeem> mSkeemList = Skeem.find(Skeem.class, "file = ?", new String[]{file.getAbsolutePath()});
if (mSkeemList.size() == 0) {
Skeem mSkeem = new Skeem();
mSkeem.setUsername(AppConstants.UserEmail);
mSkeem.setFolderName(aboslutePath);
mSkeem.setFile(file.getAbsolutePath());
uploadService uploadService = new uploadService();
uploadService.upload(mSkeem);
}
switch (event) {
case FileObserver.ALL_EVENTS:
Log.d("All", "Path" + path);
break;
case FileObserver.CREATE:
Log.d("Create", "Path" + path);
break;
}
}
}还创建了一个广播接收器,如下所示,在保持网络连接的情况下恢复文件上传
public class InternetConnector_Receiver extends BroadcastReceiver {
public InternetConnector_Receiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
try {
boolean isVisible = BaseApplication
.isActivityVisible();// Check if
// activity
// is
// visible
// or not
Log.i("Activity is Visible ", "Is activity visible : " + isVisible);
// If it is visible then trigger the task else do nothing
if (isVisible == true) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
// Check internet connection and accrding to state change the
// text of activity by calling method
if (networkInfo != null && networkInfo.isConnected()) {
Intent serviceIntent = new Intent(context, uploadService.class);
context.startService(serviceIntent);
} else {
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}并在清单中
<receiver
android:name=".InternetConnector_Receiver"
android:enabled="true">
<intent-filter>
<!-- Intent filters for broadcast receiver -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>我将状态为false的所有文件添加到数据库中。
File[] files = directory.listFiles();
for (int n = 0; n < skeemList.size(); n++) {
Skeem skeem = skeemList.get(n);
for (int o = 0; o < files.length; o++) {
if (!(skeem.getFile().equalsIgnoreCase(files[o].getAbsolutePath()))) {
if (!files[o].isDirectory()) {
Skeem mSkeem = new Skeem();
mSkeem.setFile(files[o].getAbsolutePath());
mSkeem.setFolderName(directory.getName());
mSkeem.setUsername(email);
upload(mSkeem);
}
}
}
}然后,我开始上传状态为false的文件和状态为true的更新表。当我启动应用程序时,图像上传成功开始。但过了一段时间,它就停止了。我是否使用了正确的服务从后台上传文件?有没有办法上传文件夹的内容?我已经浏览了这么多网站和链接。但我找不到我需要的确切解决方案。请帮帮我。
发布于 2018-02-01 03:05:12
使用用于服务执行的新Background Limitations,您需要非常小心地处理后台服务。我建议你像谷歌建议的那样使用JobIntentService,而不是IntentService。它充当一个IntentService,在(后)奥利奥它使用作业来做后台工作。记得请求WAKE_LOCK权限
发布于 2020-04-27 23:11:58
如果您可以使用GitHub库-
https://github.com/gotev/android-upload-service 2.3K stars 2020年4月

“-Easily在后台上传文件(多部分/二进制/FTP),并提供进度指示通知
使用FTP、HTTP multipart/form-data或二进制请求将-upload文件发送到服务器
在后台-handle多个并发上传,即使设备空闲(打瞌睡模式)
-automatically重试上传失败,可配置指数回退,上传成功后自动删除已上传文件
由该库支持的应用程序和库-
-JIRA云
-Quora
..。
“
https://stackoverflow.com/questions/48549284
复制相似问题