Skip to content

Commit

Permalink
沙盒存储
Browse files Browse the repository at this point in the history
  • Loading branch information
CeuiLiSA committed Dec 24, 2020
1 parent 21c8632 commit 855ceee
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/src/main/java/ceui/lisa/core/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ceui.lisa.download.ImageSaver;
import ceui.lisa.file.Android10DownloadFactory;
import ceui.lisa.file.LegacyFile;
import ceui.lisa.helper.Android10DownloadFactory22;
import ceui.lisa.interfaces.Callback;
import ceui.lisa.utils.Common;
import ceui.lisa.utils.Local;
Expand Down Expand Up @@ -171,7 +172,8 @@ private void downloadOne(Context context, DownloadItem bean) {
// downloadUri = Uri.fromFile(downloadFile);
// }
// }
Android10DownloadFactory factory = new Android10DownloadFactory(context, bean);
// Android10DownloadFactory factory = new Android10DownloadFactory(context, bean);
Android10DownloadFactory22 factory = new Android10DownloadFactory22(context, bean.getName());
currentIllustID = bean.getIllust().getId();
Common.showLog("Manager 下载单个 当前进度" + nonius);
uuid = bean.getUuid();
Expand Down Expand Up @@ -216,7 +218,7 @@ public void accept(Progress progress) {
downloadEntity.setIllustGson(Shaft.sGson.toJson(bean.getIllust()));
downloadEntity.setFileName(bean.getName());
downloadEntity.setDownloadTime(System.currentTimeMillis());
downloadEntity.setFilePath(factory.getUri().toString());
// downloadEntity.setFilePath(factory.getUri().toString());
AppDatabase.getAppDatabase(Shaft.getContext()).downloadDao().insert(downloadEntity);
//通知FragmentDownloadFinish 添加这一项
Intent intent = new Intent(Params.DOWNLOAD_FINISH);
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/ceui/lisa/file/Android10DownloadFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
Expand Down Expand Up @@ -38,11 +39,34 @@ public void setUri(Uri uri) {
mUri = uri;
}


public Uri getInsertUri() {
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}


@NotNull
@Override
public Uri insert(@NotNull Response response) throws IOException {
String displayName = FileCreator.createIllustFile(mDownloadItem.getIllust(),
mDownloadItem.getIndex()).getName();



String selection = MediaStore.Images.Media.DISPLAY_NAME + " = '" + displayName + "'";



// content://media/external/images/media/73404
Cursor cursor = getContext().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, selection, null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String aaa = "_data = " + cursor.getLong(0);
Common.showLog("已存在的文件 uri " + aaa + " " + cursor.getCount());
}


ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName);
values.put(MediaStore.MediaColumns.MIME_TYPE, response.body().contentType().toString());
Expand All @@ -57,6 +81,7 @@ public Uri insert(@NotNull Response response) throws IOException {
values.put(MediaStore.MediaColumns.DATA, imageFile.getPath());
}
mUri = getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

return mUri;
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/ceui/lisa/fragments/FragmentIllust.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ public void onClick(View v) {
if (illust.getPage_count() == 1) {
if (Common.isAndroidQ()) {

String displayName = FileCreator.createIllustFile(illust).getName();
String selection = MediaStore.Images.Media.DISPLAY_NAME + " = '" + displayName + "'";

Cursor cursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, selection, null, null);
if (cursor != null && cursor.getCount() > 0) {
baseBind.download.setText("已下载");
Common.showLog("cursor " + cursor.toString());
}

} else {
String displayName = FileCreator.createIllustFile(illust).getName();
String selection = MediaStore.Images.Media.DISPLAY_NAME + " = '" + displayName + "'";
Expand Down
71 changes: 71 additions & 0 deletions app/src/main/java/ceui/lisa/helper/Android10DownloadFactory22.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ceui.lisa.helper

import android.content.ContentValues
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import androidx.annotation.RequiresApi
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.PathUtils
import okhttp3.Response
import rxhttp.wrapper.callback.UriFactory
import rxhttp.wrapper.utils.query
import java.io.File
import java.io.OutputStream

class Android10DownloadFactory22 @JvmOverloads constructor(
context: Context,
private val filename: String,
private val relativePath: String = Environment.DIRECTORY_PICTURES + "/ShaftImages"
) : UriFactory(context) {

/**
* [MediaStore.Files.getContentUri]
* [MediaStore.Downloads.EXTERNAL_CONTENT_URI]
* [MediaStore.Audio.Media.EXTERNAL_CONTENT_URI]
* [MediaStore.Video.Media.EXTERNAL_CONTENT_URI]
* [MediaStore.Images.Media.EXTERNAL_CONTENT_URI]
*/
@RequiresApi(Build.VERSION_CODES.Q)
fun getInsertUri() = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

override fun query(): Uri? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getInsertUri().query(context, filename, relativePath)
} else {
val file = File("${Environment.getExternalStorageDirectory()}/$relativePath/$filename")
Uri.fromFile(file)
}
}

override fun insert(response: Response): Uri {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val uri = getInsertUri().query(context, filename, relativePath)
/*
* 通过查找,要插入的Uri已经存在,就无需再次插入
* 否则会出现新插入的文件,文件名被系统更改的现象,因为insert不会执行覆盖操作
*/
if (uri != null) {
val outputStream: OutputStream = context.contentResolver.openOutputStream(uri, "rwt")!!
outputStream.write(ByteArray(0))
outputStream.flush()
outputStream.close()
return uri
}
return ContentValues().run {
put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath) //下载到指定目录
put(MediaStore.MediaColumns.DISPLAY_NAME, filename) //文件名
//取contentType响应头作为文件类型
put(MediaStore.MediaColumns.MIME_TYPE, response.body?.contentType().toString())
context.contentResolver.insert(getInsertUri(), this)
//当相同路径下的文件,在文件管理器中被手动删除时,就会插入失败
} ?: throw NullPointerException("Uri insert failed. Try changing filename")
} else {
val file = File("${PathUtils.getExternalPicturesPath()}/ShaftImages/$filename")
FileUtils.delete(file)
return Uri.fromFile(file)
}
}
}

0 comments on commit 855ceee

Please sign in to comment.