Fix image saving path
This commit is contained in:
2 files changed
+22
-42
No files matched your search
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.hippo.ehviewer;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
|
||||
@@ -34,16 +35,14 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class AppConfig {
|
||||
|
||||
private static final String APP_DIRNAME = "EhViewer";
|
||||
public static final String APP_DIRNAME = "EhViewer";
|
||||
|
||||
private static final String DOWNLOAD = "download";
|
||||
private static final String TEMP = "temp";
|
||||
private static final String IMAGE = "image";
|
||||
private static final String PARSE_ERROR = "parse_error";
|
||||
private static final String LOGCAT = "logcat";
|
||||
private static final String DATA = "data";
|
||||
private static final String CRASH = "crash";
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static Context sContext;
|
||||
|
||||
public static void initialize(Context context) {
|
||||
@@ -59,15 +58,6 @@ public class AppConfig {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalMediaDir() {
|
||||
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
||||
File dir = new File(sContext.getExternalMediaDirs()[0], APP_DIRNAME);
|
||||
return FileUtils.ensureDirectory(dir) ? dir : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* mkdirs and get
|
||||
*/
|
||||
@@ -102,31 +92,16 @@ public class AppConfig {
|
||||
return sContext.getExternalCacheDir();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalImageDir() {
|
||||
return getExternalMediaDir();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalParseErrorDir() {
|
||||
return getDirInExternalAppDir(PARSE_ERROR);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalDataDir() {
|
||||
return getDirInExternalAppDir(DATA);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalCrashDir() {
|
||||
return getDirInExternalAppDir(CRASH);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File createExternalTempFile() {
|
||||
return FileUtils.createTempFile(getExternalTempDir(), null);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getTempDir() {
|
||||
File dir = sContext.getCacheDir();
|
||||
|
||||
@@ -83,6 +83,7 @@ import com.hippo.util.SystemUiHelper;
|
||||
import com.hippo.widget.ColorView;
|
||||
import com.hippo.yorozuya.AnimationUtils;
|
||||
import com.hippo.yorozuya.ConcurrentPool;
|
||||
import com.hippo.yorozuya.FileUtils;
|
||||
import com.hippo.yorozuya.IOUtils;
|
||||
import com.hippo.yorozuya.MathUtils;
|
||||
import com.hippo.yorozuya.ResourcesUtils;
|
||||
@@ -783,12 +784,6 @@ public class GalleryActivity extends EhActivity implements SeekBar.OnSeekBarChan
|
||||
return;
|
||||
}
|
||||
|
||||
File dir = AppConfig.getExternalImageDir();
|
||||
if (null == dir) {
|
||||
Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
mSavingPage = page;
|
||||
@@ -803,29 +798,39 @@ public class GalleryActivity extends EhActivity implements SeekBar.OnSeekBarChan
|
||||
mimeType = "image/jpeg";
|
||||
}
|
||||
|
||||
String realPath;
|
||||
ContentResolver resolver = getContentResolver();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(MediaStore.MediaColumns.DISPLAY_NAME, filename);
|
||||
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
|
||||
values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + "EhViewer");
|
||||
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + AppConfig.APP_DIRNAME);
|
||||
values.put(MediaStore.MediaColumns.IS_PENDING, 1);
|
||||
realPath = Environment.DIRECTORY_PICTURES + File.separator + AppConfig.APP_DIRNAME;
|
||||
} else {
|
||||
File path = new File(AppConfig.getExternalImageDir() + File.separator + "EhViewer");
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
path.mkdirs();
|
||||
//noinspection deprecation
|
||||
File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), AppConfig.APP_DIRNAME);
|
||||
realPath = path.toString();
|
||||
if (!FileUtils.ensureDirectory(path)) {
|
||||
Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
values.put(MediaStore.MediaColumns.DATA, path + File.separator + filename);
|
||||
}
|
||||
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
|
||||
if (!mGalleryProvider.save(page, UniFile.fromMediaUri(this, imageUri))) {
|
||||
resolver.delete(imageUri, null, null);
|
||||
Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
} else {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
ContentValues contentValues = new ContentValues();
|
||||
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0);
|
||||
resolver.update(imageUri, contentValues, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
Toast.makeText(this, getString(R.string.image_saved, imageUri.toString()), Toast.LENGTH_SHORT).show();
|
||||
// Sync media store
|
||||
//sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, file.getUri()));
|
||||
Toast.makeText(this, getString(R.string.image_saved, realPath), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user