调试到下载图片,未调试图片存储管理。
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Fri Nov 28 18:45:48 GMT 2025
|
#Sat Nov 29 02:59:10 GMT 2025
|
||||||
stageCount=11
|
stageCount=11
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.11
|
baseVersion=15.11
|
||||||
publishVersion=15.11.10
|
publishVersion=15.11.10
|
||||||
buildCount=29
|
buildCount=32
|
||||||
baseBetaVersion=15.11.11
|
baseBetaVersion=15.11.11
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import cc.winboll.studio.powerbell.activities.WinBoLLActivity;
|
|||||||
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
||||||
import cc.winboll.studio.powerbell.fragments.MainViewFragment;
|
import cc.winboll.studio.powerbell.fragments.MainViewFragment;
|
||||||
import cc.winboll.studio.powerbell.unittest.MainUnitTestActivity;
|
import cc.winboll.studio.powerbell.unittest.MainUnitTestActivity;
|
||||||
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
|
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 主活动类(修复小米广告SDK空Context崩溃问题)
|
* 主活动类(修复小米广告SDK空Context崩溃问题)
|
||||||
@@ -281,7 +281,7 @@ public class MainActivity extends WinBoLLActivity {
|
|||||||
if (isFinishing() || isDestroyed()) {
|
if (isFinishing() || isDestroyed()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this);
|
||||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||||
int nPixelColor = bean.getPixelColor();
|
int nPixelColor = bean.getPixelColor();
|
||||||
RelativeLayout mainLayout = findViewById(R.id.activitymainRelativeLayout1);
|
RelativeLayout mainLayout = findViewById(R.id.activitymainRelativeLayout1);
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import cc.winboll.studio.powerbell.R;
|
|||||||
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
||||||
import cc.winboll.studio.powerbell.dialogs.BackgroundPicturePreviewDialog;
|
import cc.winboll.studio.powerbell.dialogs.BackgroundPicturePreviewDialog;
|
||||||
import cc.winboll.studio.powerbell.dialogs.NetworkBackgroundDialog;
|
import cc.winboll.studio.powerbell.dialogs.NetworkBackgroundDialog;
|
||||||
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
|
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||||
import cc.winboll.studio.powerbell.utils.FileUtils;
|
import cc.winboll.studio.powerbell.utils.FileUtils;
|
||||||
import cc.winboll.studio.powerbell.utils.UriUtil;
|
import cc.winboll.studio.powerbell.utils.UriUtil;
|
||||||
import cc.winboll.studio.powerbell.views.BackgroundView;
|
import cc.winboll.studio.powerbell.views.BackgroundView;
|
||||||
@@ -40,7 +40,7 @@ import androidx.core.util.Preconditions;
|
|||||||
public class BackgroundPictureActivity extends WinBoLLActivity implements BackgroundPicturePreviewDialog.IOnRecivedPictureListener {
|
public class BackgroundPictureActivity extends WinBoLLActivity implements BackgroundPicturePreviewDialog.IOnRecivedPictureListener {
|
||||||
|
|
||||||
public static final String TAG = "BackgroundPictureActivity";
|
public static final String TAG = "BackgroundPictureActivity";
|
||||||
public BackgroundPictureUtils mBackgroundPictureUtils;
|
public BackgroundSourceUtils mBackgroundPictureUtils;
|
||||||
|
|
||||||
// 图片选择请求码
|
// 图片选择请求码
|
||||||
public static final int REQUEST_SELECT_PICTURE = 0;
|
public static final int REQUEST_SELECT_PICTURE = 0;
|
||||||
@@ -55,7 +55,8 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
private File mfRecivedPicture; // 接收的图片文件
|
private File mfRecivedPicture; // 接收的图片文件
|
||||||
|
|
||||||
// 背景视图预览图片的文件名
|
// 背景视图预览图片的文件名
|
||||||
private String preViewFilePathBackgroundView = "";
|
private String preViewFilePath = "";
|
||||||
|
private String preViewFileUrl = "";
|
||||||
BackgroundView bvPreviewBackground;
|
BackgroundView bvPreviewBackground;
|
||||||
boolean isCommitSettings = false;
|
boolean isCommitSettings = false;
|
||||||
|
|
||||||
@@ -72,7 +73,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
private static String _mSourceCroppedFilePath;
|
private static String _mSourceCroppedFilePath;
|
||||||
private static String _mszCommonFileType = "jpeg";
|
private static String _mszCommonFileType = "jpeg";
|
||||||
private int mnPictureCompress = 100;
|
private int mnPictureCompress = 100;
|
||||||
private static String _RecivedPictureFileName;
|
private static String _RecivedBackgroundFileName;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Activity getActivity() {
|
public Activity getActivity() {
|
||||||
@@ -91,7 +92,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
initEnv();
|
initEnv();
|
||||||
|
|
||||||
// 初始化工具类和文件夹
|
// 初始化工具类和文件夹
|
||||||
mBackgroundPictureUtils = BackgroundPictureUtils.getInstance(this);
|
mBackgroundPictureUtils = BackgroundSourceUtils.getInstance(this);
|
||||||
mfBackgroundDir = new File(mBackgroundPictureUtils.getBackgroundDir());
|
mfBackgroundDir = new File(mBackgroundPictureUtils.getBackgroundDir());
|
||||||
if (!mfBackgroundDir.exists()) {
|
if (!mfBackgroundDir.exists()) {
|
||||||
mfBackgroundDir.mkdirs();
|
mfBackgroundDir.mkdirs();
|
||||||
@@ -149,7 +150,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
|
|
||||||
private void initEnv() {
|
private void initEnv() {
|
||||||
LogUtils.d(TAG, "initEnv()");
|
LogUtils.d(TAG, "initEnv()");
|
||||||
_RecivedPictureFileName = "SourcePicture.data";
|
_RecivedBackgroundFileName = "SourcePicture.data";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getBackgroundFileName() {
|
public static String getBackgroundFileName() {
|
||||||
@@ -158,7 +159,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAcceptRecivedPicture(String szPreRecivedPictureName) {
|
public void onAcceptRecivedPicture(String szPreRecivedPictureName) {
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this);
|
||||||
utils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
|
utils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
|
||||||
utils.saveData();
|
utils.saveData();
|
||||||
|
|
||||||
@@ -178,7 +179,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
LogUtils.d(TAG, String.format("_mSourceCroppedFilePath : %s", _mSourceCroppedFilePath));
|
LogUtils.d(TAG, String.format("_mSourceCroppedFilePath : %s", _mSourceCroppedFilePath));
|
||||||
//ImageView ivPreviewBackground = (ImageView) findViewById(R.id.activitybackgroundpictureImageView1);
|
//ImageView ivPreviewBackground = (ImageView) findViewById(R.id.activitybackgroundpictureImageView1);
|
||||||
bvPreviewBackground = (BackgroundView) findViewById(R.id.activitybackgroundpictureBackgroundView1);
|
bvPreviewBackground = (BackgroundView) findViewById(R.id.activitybackgroundpictureBackgroundView1);
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this);
|
||||||
utils.loadBackgroundPictureBean();
|
utils.loadBackgroundPictureBean();
|
||||||
|
|
||||||
boolean isUseBackgroundFile = utils.getBackgroundPictureBean().isUseBackgroundFile();
|
boolean isUseBackgroundFile = utils.getBackgroundPictureBean().isUseBackgroundFile();
|
||||||
@@ -188,9 +189,9 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
if (_mSourceCroppedFile.exists()) {
|
if (_mSourceCroppedFile.exists()) {
|
||||||
//try {
|
//try {
|
||||||
//String filePath = utils.getBackgroundDir() + getBackgroundFileName();
|
//String filePath = utils.getBackgroundDir() + getBackgroundFileName();
|
||||||
preViewFilePathBackgroundView = _mSourceCroppedFilePath;
|
preViewFilePath = _mSourceCroppedFilePath;
|
||||||
LogUtils.d(TAG, String.format("preViewFilePathBackgroundView : %s", preViewFilePathBackgroundView));
|
LogUtils.d(TAG, String.format("preViewFilePathBackgroundView : %s", preViewFilePath));
|
||||||
bvPreviewBackground.previewBackgroundImage(preViewFilePathBackgroundView);
|
bvPreviewBackground.previewBackgroundImage(preViewFilePath);
|
||||||
/*Drawable drawable = FileUtils.getImageDrawable(filePath);
|
/*Drawable drawable = FileUtils.getImageDrawable(filePath);
|
||||||
if (drawable != null) {
|
if (drawable != null) {
|
||||||
//drawable.setAlpha(120);
|
//drawable.setAlpha(120);
|
||||||
@@ -203,8 +204,8 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
// }
|
// }
|
||||||
} else {
|
} else {
|
||||||
ToastUtils.show("未使用背景图片");
|
ToastUtils.show("未使用背景图片");
|
||||||
preViewFilePathBackgroundView = "";
|
preViewFilePath = "";
|
||||||
bvPreviewBackground.previewBackgroundImage(preViewFilePathBackgroundView);
|
bvPreviewBackground.previewBackgroundImage(preViewFilePath);
|
||||||
// Drawable drawable = getResources().getDrawable(R.drawable.blank10x10);
|
// Drawable drawable = getResources().getDrawable(R.drawable.blank10x10);
|
||||||
// if (drawable != null) {
|
// if (drawable != null) {
|
||||||
// drawable.setAlpha(120);
|
// drawable.setAlpha(120);
|
||||||
@@ -217,13 +218,13 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
private View.OnClickListener onOriginNullClickListener = new View.OnClickListener() {
|
private View.OnClickListener onOriginNullClickListener = new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this);
|
||||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||||
bean.setIsUseBackgroundFile(false);
|
bean.setIsUseBackgroundFile(false);
|
||||||
utils.saveData();
|
utils.saveData();
|
||||||
|
|
||||||
preViewFilePathBackgroundView = "";
|
preViewFilePath = "";
|
||||||
bvPreviewBackground.previewBackgroundImage(preViewFilePathBackgroundView);
|
bvPreviewBackground.previewBackgroundImage(preViewFilePath);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -288,7 +289,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
private View.OnClickListener onReceivedPictureClickListener = new View.OnClickListener() {
|
private View.OnClickListener onReceivedPictureClickListener = new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this);
|
||||||
utils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
|
utils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
|
||||||
utils.saveData();
|
utils.saveData();
|
||||||
updatePreviewBackground();
|
updatePreviewBackground();
|
||||||
@@ -311,7 +312,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
private View.OnClickListener onCleanPixelClickListener = new View.OnClickListener() {
|
private View.OnClickListener onCleanPixelClickListener = new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this);
|
||||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||||
bean.setPixelColor(0);
|
bean.setPixelColor(0);
|
||||||
utils.saveData();
|
utils.saveData();
|
||||||
@@ -446,27 +447,24 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
fos.flush();
|
fos.flush();
|
||||||
if (success) {
|
if (success) {
|
||||||
//ToastUtils.show("保存成功");
|
//ToastUtils.show("保存成功");
|
||||||
// 更新数据
|
|
||||||
// BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
|
||||||
// BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
|
||||||
// bean.setIsUseBackgroundFile(true);
|
|
||||||
// utils.saveData();
|
|
||||||
updatePreviewBackground();
|
updatePreviewBackground();
|
||||||
} else {
|
} else {
|
||||||
|
LogUtils.e(TAG, "图片压缩保存失败");
|
||||||
ToastUtils.show("图片压缩保存失败");
|
ToastUtils.show("图片压缩保存失败");
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
LogUtils.e(TAG, "文件未找到" + e);
|
LogUtils.e(TAG, "文件未找到" + e);
|
||||||
ToastUtils.show("保存失败:文件路径错误");
|
ToastUtils.show("文件未找到" + e);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogUtils.e(TAG, "写入异常" + e);
|
LogUtils.e(TAG, "写入异常" + e);
|
||||||
ToastUtils.show("保存失败:磁盘可能已满或路径错误");
|
ToastUtils.show("写入异常" + e);
|
||||||
} finally {
|
} finally {
|
||||||
if (fos != null) {
|
if (fos != null) {
|
||||||
try {
|
try {
|
||||||
fos.close();
|
fos.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogUtils.e(TAG, "流关闭异常" + e);
|
LogUtils.e(TAG, "流关闭异常" + e);
|
||||||
|
ToastUtils.show("流关闭异常" + e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (scaledBitmap != null && !scaledBitmap.isRecycled()) {
|
if (scaledBitmap != null && !scaledBitmap.isRecycled()) {
|
||||||
@@ -500,15 +498,16 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
}
|
}
|
||||||
|
|
||||||
public File getRecivedPictureFile() {
|
public File getRecivedPictureFile() {
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this);
|
||||||
utils.loadBackgroundPictureBean();
|
utils.loadBackgroundPictureBean();
|
||||||
return new File(utils.getBackgroundDir(), _RecivedPictureFileName);
|
return new File(utils.getBackgroundDir(), _RecivedBackgroundFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveToRecivedPictureFile(String srcFilePath) {
|
public void saveToRecivedBackground(String srcFilePath, String srcFillSourcePath) {
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this);
|
||||||
utils.loadBackgroundPictureBean();
|
utils.loadBackgroundPictureBean();
|
||||||
File dstFile = new File(utils.getBackgroundDir(), _RecivedPictureFileName);
|
File dstFile = new File(utils.getBackgroundDir(), _RecivedBackgroundFileName);
|
||||||
|
compressQualityToRecivedPicture(srcFilePath);
|
||||||
FileUtils.copyFile(new File(srcFilePath), dstFile);
|
FileUtils.copyFile(new File(srcFilePath), dstFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -619,7 +618,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setBackgroundColor() {
|
void setBackgroundColor() {
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this);
|
||||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||||
int nPixelColor = bean.getPixelColor();
|
int nPixelColor = bean.getPixelColor();
|
||||||
RelativeLayout mainLayout = findViewById(R.id.activitybackgroundpictureRelativeLayout1);
|
RelativeLayout mainLayout = findViewById(R.id.activitybackgroundpictureRelativeLayout1);
|
||||||
@@ -636,27 +635,17 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
// 在需要显示对话框的地方(如网络状态监听回调中)
|
// 在需要显示对话框的地方(如网络状态监听回调中)
|
||||||
NetworkBackgroundDialog dialog = new NetworkBackgroundDialog(this, new NetworkBackgroundDialog.OnDialogClickListener() {
|
NetworkBackgroundDialog dialog = new NetworkBackgroundDialog(this, new NetworkBackgroundDialog.OnDialogClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onConfirm(String szConfirmFilePath) {
|
public void onConfirm(String szConfirmFilePath, String szConfirmFileUrl) {
|
||||||
//ToastUtils.show("onConfirm");
|
//ToastUtils.show("onConfirm");
|
||||||
onRecivedPictureListener.onRecivedPicture(szConfirmFilePath);
|
// 保存预览资源信息
|
||||||
|
preViewFilePath = szConfirmFilePath;
|
||||||
//((BackgroundPicturePreviewDialog.IOnRecivedPictureListener)this).onAcceptRecivedPicture(szConfirmFilePath);
|
preViewFileUrl = szConfirmFileUrl;
|
||||||
|
onRecivedPictureListener.onRecivedPicture(preViewFilePath, preViewFileUrl);
|
||||||
// 设置预览图
|
|
||||||
preViewFilePathBackgroundView = szConfirmFilePath;
|
|
||||||
bvPreviewBackground.previewBackgroundImage(preViewFilePathBackgroundView);
|
|
||||||
|
|
||||||
// 处理确认逻辑(如允许后台网络使用)
|
|
||||||
LogUtils.d("MainActivity", "用户允许后台网络使用");
|
|
||||||
// 执行具体业务:如开启后台网络请求服务
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCancel() {
|
public void onCancel() {
|
||||||
//ToastUtils.show("onCancel");
|
//ToastUtils.show("onCancel");
|
||||||
// 处理取消逻辑(如禁止后台网络使用)
|
|
||||||
LogUtils.d("MainActivity", "用户禁止后台网络使用");
|
|
||||||
// 执行具体业务:如关闭后台网络请求
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -670,13 +659,13 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface OnRecivedPictureListener {
|
interface OnRecivedPictureListener {
|
||||||
void onRecivedPicture(String srcFilePath);
|
void onRecivedPicture(String srcFilePath, String srcFileUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
OnRecivedPictureListener onRecivedPictureListener = new OnRecivedPictureListener(){
|
OnRecivedPictureListener onRecivedPictureListener = new OnRecivedPictureListener(){
|
||||||
@Override
|
@Override
|
||||||
public void onRecivedPicture(String srcFilePath) {
|
public void onRecivedPicture(String srcFilePath, String srcFileUrl) {
|
||||||
saveToRecivedPictureFile(srcFilePath);
|
saveToRecivedBackground(srcFilePath, srcFileUrl);
|
||||||
startCropImageActivity(true);
|
startCropImageActivity(true);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -692,16 +681,16 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr
|
|||||||
@Override
|
@Override
|
||||||
public void onNo() {
|
public void onNo() {
|
||||||
isCommitSettings = true;
|
isCommitSettings = true;
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this);
|
||||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||||
bean.setIsUseBackgroundFile(!preViewFilePathBackgroundView.equals(""));
|
bean.setIsUseBackgroundFile(!preViewFilePath.equals(""));
|
||||||
utils.saveData();
|
utils.saveData();
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onYes() {
|
public void onYes() {
|
||||||
bvPreviewBackground.setImageViewSource(preViewFilePathBackgroundView);
|
bvPreviewBackground.setImageViewSource(preViewFilePath);
|
||||||
isCommitSettings = true;
|
isCommitSettings = true;
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import cc.winboll.studio.powerbell.R;
|
|||||||
import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity;
|
import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity;
|
||||||
import cc.winboll.studio.powerbell.activities.PixelPickerActivity;
|
import cc.winboll.studio.powerbell.activities.PixelPickerActivity;
|
||||||
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
||||||
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
|
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@@ -193,7 +193,7 @@ public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActi
|
|||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
// 可以在这里添加确定后的回调逻辑
|
// 可以在这里添加确定后的回调逻辑
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(PixelPickerActivity.this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(PixelPickerActivity.this);
|
||||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||||
bean.setPixelColor(pixelColor);
|
bean.setPixelColor(pixelColor);
|
||||||
utils.saveData();
|
utils.saveData();
|
||||||
@@ -217,7 +217,7 @@ public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActi
|
|||||||
|
|
||||||
|
|
||||||
void setBackgroundColor() {
|
void setBackgroundColor() {
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(PixelPickerActivity.this);
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(PixelPickerActivity.this);
|
||||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||||
int nPixelColor = bean.getPixelColor();
|
int nPixelColor = bean.getPixelColor();
|
||||||
RelativeLayout mainLayout = findViewById(R.id.activitypixelpickerRelativeLayout1);
|
RelativeLayout mainLayout = findViewById(R.id.activitypixelpickerRelativeLayout1);
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import cc.winboll.studio.libappbase.LogUtils;
|
|||||||
import cc.winboll.studio.powerbell.MainActivity;
|
import cc.winboll.studio.powerbell.MainActivity;
|
||||||
import cc.winboll.studio.powerbell.R;
|
import cc.winboll.studio.powerbell.R;
|
||||||
import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity;
|
import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity;
|
||||||
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
|
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||||
import cc.winboll.studio.powerbell.utils.FileUtils;
|
import cc.winboll.studio.powerbell.utils.FileUtils;
|
||||||
import cc.winboll.studio.powerbell.utils.UriUtil;
|
import cc.winboll.studio.powerbell.utils.UriUtil;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -29,7 +29,7 @@ public class BackgroundPicturePreviewDialog extends Dialog {
|
|||||||
public static final String TAG = "BackgroundPicturePreviewDialog";
|
public static final String TAG = "BackgroundPicturePreviewDialog";
|
||||||
|
|
||||||
Context mContext;
|
Context mContext;
|
||||||
BackgroundPictureUtils mBackgroundPictureUtils;
|
BackgroundSourceUtils mBackgroundPictureUtils;
|
||||||
Button dialogbackgroundpicturepreviewButton1;
|
Button dialogbackgroundpicturepreviewButton1;
|
||||||
Button dialogbackgroundpicturepreviewButton2;
|
Button dialogbackgroundpicturepreviewButton2;
|
||||||
String mszPreReceivedFileName;
|
String mszPreReceivedFileName;
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ import androidx.appcompat.app.AlertDialog;
|
|||||||
import cc.winboll.studio.libappbase.LogUtils;
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
import cc.winboll.studio.libappbase.ToastUtils;
|
import cc.winboll.studio.libappbase.ToastUtils;
|
||||||
import cc.winboll.studio.powerbell.R;
|
import cc.winboll.studio.powerbell.R;
|
||||||
import cc.winboll.studio.powerbell.utils.PictureUtils;
|
|
||||||
import cc.winboll.studio.powerbell.views.BackgroundView;
|
import cc.winboll.studio.powerbell.views.BackgroundView;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import cc.winboll.studio.powerbell.utils.ImageDownloader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||||
@@ -46,10 +46,11 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
|||||||
// 主线程 Handler,用于接收子线程消息并更新 UI
|
// 主线程 Handler,用于接收子线程消息并更新 UI
|
||||||
private Handler mUiHandler;
|
private Handler mUiHandler;
|
||||||
String previewFilePath;
|
String previewFilePath;
|
||||||
|
String previewFileUrl;
|
||||||
|
|
||||||
// 按钮点击回调接口(Java7 接口实现)
|
// 按钮点击回调接口(Java7 接口实现)
|
||||||
public interface OnDialogClickListener {
|
public interface OnDialogClickListener {
|
||||||
void onConfirm(String szConfirmFilePath); // 确认按钮点击
|
void onConfirm(String szConfirmFilePath, String previewFileUrl); // 确认按钮点击
|
||||||
void onCancel(); // 取消按钮点击
|
void onCancel(); // 取消按钮点击
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,8 +88,8 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
|||||||
switch (msg.what) {
|
switch (msg.what) {
|
||||||
case MSG_IMAGE_LOAD_SUCCESS:
|
case MSG_IMAGE_LOAD_SUCCESS:
|
||||||
// 图片加载成功,获取文件路径并设置背景
|
// 图片加载成功,获取文件路径并设置背景
|
||||||
String filePath = (String) msg.obj;
|
previewFilePath = (String) msg.obj;
|
||||||
setBackgroundFromPath(filePath);
|
setBackgroundFromPath(previewFilePath);
|
||||||
break;
|
break;
|
||||||
case MSG_IMAGE_LOAD_FAILED:
|
case MSG_IMAGE_LOAD_FAILED:
|
||||||
// 图片加载失败,设置默认背景
|
// 图片加载失败,设置默认背景
|
||||||
@@ -135,7 +136,8 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
|||||||
btnPreview = (Button) dialogView.findViewById(R.id.btn_preview);
|
btnPreview = (Button) dialogView.findViewById(R.id.btn_preview);
|
||||||
etURL = (EditText) dialogView.findViewById(R.id.et_url);
|
etURL = (EditText) dialogView.findViewById(R.id.et_url);
|
||||||
bvBackgroundPreview = (BackgroundView) dialogView.findViewById(R.id.bv_background_preview);
|
bvBackgroundPreview = (BackgroundView) dialogView.findViewById(R.id.bv_background_preview);
|
||||||
|
// 加载初始图片
|
||||||
|
bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher);
|
||||||
// 设置按钮点击事件
|
// 设置按钮点击事件
|
||||||
setButtonClickListeners();
|
setButtonClickListeners();
|
||||||
}
|
}
|
||||||
@@ -166,7 +168,7 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
|||||||
|
|
||||||
dismiss(); // 关闭对话框
|
dismiss(); // 关闭对话框
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
listener.onConfirm(previewFilePath);
|
listener.onConfirm(previewFilePath, previewFileUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -175,14 +177,7 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
|||||||
btnPreview.setOnClickListener(new View.OnClickListener() {
|
btnPreview.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
LogUtils.d("NetworkBackgroundDialog", "确认预览点击");
|
|
||||||
downloadImageToAlbumAndPreview();
|
downloadImageToAlbumAndPreview();
|
||||||
/*String url = etURL.getText().toString().trim();
|
|
||||||
if (url.isEmpty()) {
|
|
||||||
ToastUtils.show("请输入图片链接");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ImageDownloader.getInstance(mContext).downloadImage(url, mDownloadCallback);*/
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -197,8 +192,7 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
|||||||
File imageFile = new File(filePath);
|
File imageFile = new File(filePath);
|
||||||
if (!imageFile.exists()) {
|
if (!imageFile.exists()) {
|
||||||
LogUtils.e(TAG, "图片文件不存在:" + filePath);
|
LogUtils.e(TAG, "图片文件不存在:" + filePath);
|
||||||
ToastUtils.show("Test");
|
bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher);
|
||||||
//bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,29 +243,10 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
|||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*ImageDownloader.DownloadCallback mDownloadCallback = new ImageDownloader.DownloadCallback() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(String filePath) {
|
|
||||||
ToastUtils.show("图片下载成功:" + filePath);
|
|
||||||
LogUtils.d(TAG, filePath);
|
|
||||||
// 发送消息到主线程,携带图片路径
|
|
||||||
Message successMsg = mUiHandler.obtainMessage(MSG_IMAGE_LOAD_SUCCESS, filePath);
|
|
||||||
mUiHandler.sendMessage(successMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(String errorMsg) {
|
|
||||||
ToastUtils.show("下载失败:" + errorMsg);
|
|
||||||
LogUtils.e(TAG, errorMsg);
|
|
||||||
// 发送图片加载失败消息
|
|
||||||
mUiHandler.sendEmptyMessage(MSG_IMAGE_LOAD_FAILED);
|
|
||||||
}
|
|
||||||
};*/
|
|
||||||
|
|
||||||
void downloadImageToAlbumAndPreview() {
|
void downloadImageToAlbumAndPreview() {
|
||||||
//String imgUrl = "https://example.com/test.jpg";
|
//String previewFileUrl = "https://example.com/test.jpg";
|
||||||
String imgUrl = etURL.getText().toString();
|
previewFileUrl = etURL.getText().toString();
|
||||||
PictureUtils.downloadImageToAlbum(mContext, imgUrl, new PictureUtils.DownloadCallback(){
|
ImageDownloader.getInstance(mContext).downloadImage(previewFileUrl, new ImageDownloader.DownloadCallback(){
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(String savePath) {
|
public void onSuccess(String savePath) {
|
||||||
ToastUtils.show("下载成功:" + savePath);
|
ToastUtils.show("下载成功:" + savePath);
|
||||||
@@ -281,8 +256,8 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Exception e) {
|
public void onFailure(String errorMsg) {
|
||||||
ToastUtils.show("下载失败:" + e.getMessage());
|
ToastUtils.show("下载失败:" + errorMsg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import cc.winboll.studio.powerbell.activities.PixelPickerActivity;
|
|||||||
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
||||||
import cc.winboll.studio.powerbell.services.ControlCenterService;
|
import cc.winboll.studio.powerbell.services.ControlCenterService;
|
||||||
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
|
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
|
||||||
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
|
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||||
import cc.winboll.studio.powerbell.utils.ServiceUtils;
|
import cc.winboll.studio.powerbell.utils.ServiceUtils;
|
||||||
import cc.winboll.studio.powerbell.views.BackgroundView;
|
import cc.winboll.studio.powerbell.views.BackgroundView;
|
||||||
import cc.winboll.studio.powerbell.views.BatteryDrawable;
|
import cc.winboll.studio.powerbell.views.BatteryDrawable;
|
||||||
@@ -83,7 +83,7 @@ public class MainViewFragment extends Fragment {
|
|||||||
// 获取指定ID的View实例
|
// 获取指定ID的View实例
|
||||||
bvPreviewBackground = mView.findViewById(R.id.fragmentmainviewBackgroundView1);
|
bvPreviewBackground = mView.findViewById(R.id.fragmentmainviewBackgroundView1);
|
||||||
|
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(getActivity());
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(getActivity());
|
||||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||||
int nPixelColor = bean.getPixelColor();
|
int nPixelColor = bean.getPixelColor();
|
||||||
bvPreviewBackground.setBackgroundColor(nPixelColor);
|
bvPreviewBackground.setBackgroundColor(nPixelColor);
|
||||||
@@ -152,7 +152,7 @@ public class MainViewFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(getActivity());
|
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(getActivity());
|
||||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||||
int nPixelColor = bean.getPixelColor();
|
int nPixelColor = bean.getPixelColor();
|
||||||
bvPreviewBackground.setBackgroundColor(nPixelColor);
|
bvPreviewBackground.setBackgroundColor(nPixelColor);
|
||||||
|
|||||||
@@ -9,26 +9,26 @@ import android.content.Context;
|
|||||||
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public class BackgroundPictureUtils {
|
public class BackgroundSourceUtils {
|
||||||
|
|
||||||
public static final String TAG = "BackgroundPictureUtils";
|
public static final String TAG = "BackgroundPictureUtils";
|
||||||
|
|
||||||
static BackgroundPictureUtils _mBackgroundPictureUtils;
|
static BackgroundSourceUtils _mBackgroundPictureUtils;
|
||||||
Context mContext;
|
Context mContext;
|
||||||
BackgroundPictureBean mBackgroundPictureBean;
|
BackgroundPictureBean mBackgroundPictureBean;
|
||||||
// 背景图片目录
|
// 背景图片目录
|
||||||
String mszBackgroundDir;
|
String mszBackgroundDir;
|
||||||
|
|
||||||
BackgroundPictureUtils(Context context) {
|
BackgroundSourceUtils(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
String szExternalFilesDir = mContext.getExternalFilesDir(TAG) + File.separator;
|
String szExternalFilesDir = mContext.getExternalFilesDir(TAG) + File.separator;
|
||||||
setBackgroundDir(szExternalFilesDir + "Background" + File.separator);
|
setBackgroundDir(szExternalFilesDir + "Background" + File.separator);
|
||||||
loadBackgroundPictureBean();
|
loadBackgroundPictureBean();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BackgroundPictureUtils getInstance(Context context) {
|
public static BackgroundSourceUtils getInstance(Context context) {
|
||||||
if (_mBackgroundPictureUtils == null) {
|
if (_mBackgroundPictureUtils == null) {
|
||||||
_mBackgroundPictureUtils = new BackgroundPictureUtils(context);
|
_mBackgroundPictureUtils = new BackgroundSourceUtils(context);
|
||||||
}
|
}
|
||||||
return _mBackgroundPictureUtils;
|
return _mBackgroundPictureUtils;
|
||||||
}
|
}
|
||||||
@@ -1,207 +0,0 @@
|
|||||||
package cc.winboll.studio.powerbell.utils;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.Environment;
|
|
||||||
import android.util.Log;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Locale;
|
|
||||||
import okhttp3.Call;
|
|
||||||
import okhttp3.Callback;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
|
||||||
* @Date 2025/11/21 18:55
|
|
||||||
* @Describe
|
|
||||||
* 图片下载工具类(指定目录保存:Pictures/PowerBell/BackgroundHistory)
|
|
||||||
*/
|
|
||||||
public class PictureUtils {
|
|
||||||
private static final String TAG = "PictureUtils";
|
|
||||||
private static final String ROOT_DIR = "PowerBell/BackgroundHistory"; // 自定义目录结构
|
|
||||||
private static OkHttpClient sOkHttpClient;
|
|
||||||
|
|
||||||
static {
|
|
||||||
sOkHttpClient = new OkHttpClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 下载网络图片到指定目录(外部存储/Pictures/PowerBell/BackgroundHistory)
|
|
||||||
* @param context 上下文(用于通知相册刷新)
|
|
||||||
* @param imgUrl 图片网络URL
|
|
||||||
* @param callback 下载结果回调(成功/失败)
|
|
||||||
*/
|
|
||||||
public static void downloadImageToAlbum(final Context context, final String imgUrl, final DownloadCallback callback) {
|
|
||||||
// 检查参数合法性
|
|
||||||
if (context == null) {
|
|
||||||
if (callback != null) {
|
|
||||||
callback.onFailure(new IllegalArgumentException("Context不能为空"));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (imgUrl == null || imgUrl.isEmpty()) {
|
|
||||||
if (callback != null) {
|
|
||||||
callback.onFailure(new IllegalArgumentException("图片URL为空"));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
startDownload(context, imgUrl, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行实际的下载逻辑
|
|
||||||
*/
|
|
||||||
private static void startDownload(final Context context, final String imgUrl, final DownloadCallback callback) {
|
|
||||||
Request request = new Request.Builder().url(imgUrl).build();
|
|
||||||
sOkHttpClient.newCall(request).enqueue(new Callback() {
|
|
||||||
@Override
|
|
||||||
public void onResponse(Call call, Response response) throws IOException {
|
|
||||||
if (!response.isSuccessful()) {
|
|
||||||
if (callback != null) {
|
|
||||||
callback.onFailure(new IOException("请求失败,响应码:" + response.code()));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
InputStream inputStream = null;
|
|
||||||
FileOutputStream outputStream = null;
|
|
||||||
try {
|
|
||||||
inputStream = response.body().byteStream();
|
|
||||||
// 1. 获取并创建指定保存目录(外部存储/Pictures/PowerBell/BackgroundHistory)
|
|
||||||
File saveDir = getTargetSaveDir(context);
|
|
||||||
if (!saveDir.exists()) {
|
|
||||||
boolean isDirCreated = saveDir.mkdirs(); // 递归创建多级目录
|
|
||||||
if (!isDirCreated) {
|
|
||||||
if (callback != null) {
|
|
||||||
callback.onFailure(new IOException("创建目录失败:" + saveDir.getAbsolutePath()));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. 解析图片后缀
|
|
||||||
String fileSuffix = getImageSuffix(imgUrl, response);
|
|
||||||
// 3. 生成时间戳文件名
|
|
||||||
String fileName = generateTimeFileName() + fileSuffix;
|
|
||||||
// 4. 创建文件
|
|
||||||
final File saveFile = new File(saveDir, fileName);
|
|
||||||
|
|
||||||
// 5. 写入文件
|
|
||||||
outputStream = new FileOutputStream(saveFile);
|
|
||||||
byte[] buffer = new byte[1024 * 4];
|
|
||||||
int len;
|
|
||||||
while ((len = inputStream.read(buffer)) != -1) {
|
|
||||||
outputStream.write(buffer, 0, len);
|
|
||||||
}
|
|
||||||
outputStream.flush();
|
|
||||||
|
|
||||||
// 6. 通知相册刷新(使图片显示在系统相册中)
|
|
||||||
notifyAlbumRefresh(context, saveFile);
|
|
||||||
|
|
||||||
// 成功回调
|
|
||||||
if (callback != null) {
|
|
||||||
callback.onSuccess(saveFile.getAbsolutePath());
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e(TAG, "下载图片异常", e);
|
|
||||||
if (callback != null) {
|
|
||||||
callback.onFailure(e);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
// 关闭资源
|
|
||||||
if (inputStream != null) inputStream.close();
|
|
||||||
if (outputStream != null) outputStream.close();
|
|
||||||
if (response.body() != null) response.body().close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(Call call, final IOException e) {
|
|
||||||
Log.e(TAG, "下载图片失败", e);
|
|
||||||
if (callback != null) {
|
|
||||||
callback.onFailure(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取目标保存目录:外部存储/Pictures/PowerBell/BackgroundHistory
|
|
||||||
*/
|
|
||||||
private static File getTargetSaveDir(Context context) {
|
|
||||||
// 优先使用公共Pictures目录(兼容多数设备)
|
|
||||||
File publicPicturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
|
||||||
if (publicPicturesDir.exists()) {
|
|
||||||
return new File(publicPicturesDir, ROOT_DIR);
|
|
||||||
}
|
|
||||||
// 备选:应用私有Pictures目录(若公共目录不可用)
|
|
||||||
File appPicturesDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
|
|
||||||
if (appPicturesDir != null) {
|
|
||||||
return new File(appPicturesDir, ROOT_DIR);
|
|
||||||
}
|
|
||||||
// 极端情况:外部存储根目录
|
|
||||||
return new File(Environment.getExternalStorageDirectory(), ROOT_DIR);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 解析图片后缀名
|
|
||||||
*/
|
|
||||||
private static String getImageSuffix(String imgUrl, Response response) {
|
|
||||||
// 优先从URL解析
|
|
||||||
if (imgUrl.lastIndexOf('.') != -1) {
|
|
||||||
String suffix = imgUrl.substring(imgUrl.lastIndexOf('.'));
|
|
||||||
if (suffix.length() <= 5 && (suffix.contains("png") || suffix.contains("jpg") || suffix.contains("jpeg") || suffix.contains("gif"))) {
|
|
||||||
return suffix.toLowerCase(Locale.getDefault());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 从响应头解析
|
|
||||||
String contentType = response.header("Content-Type");
|
|
||||||
if (contentType != null) {
|
|
||||||
if (contentType.contains("png")) return ".png";
|
|
||||||
if (contentType.contains("jpeg") || contentType.contains("jpg")) return ".jpg";
|
|
||||||
if (contentType.contains("gif")) return ".gif";
|
|
||||||
}
|
|
||||||
return ".jpg"; // 默认后缀
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成时间戳文件名
|
|
||||||
*/
|
|
||||||
private static String generateTimeFileName() {
|
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.getDefault());
|
|
||||||
return sdf.format(new Date());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 通知相册刷新
|
|
||||||
*/
|
|
||||||
private static void notifyAlbumRefresh(Context context, File file) {
|
|
||||||
try {
|
|
||||||
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
|
|
||||||
Uri uri = Uri.fromFile(file);
|
|
||||||
intent.setData(uri);
|
|
||||||
context.sendBroadcast(intent);
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e(TAG, "通知相册刷新失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 下载结果回调接口
|
|
||||||
*/
|
|
||||||
public interface DownloadCallback {
|
|
||||||
void onSuccess(String savePath); // 下载成功(子线程回调)
|
|
||||||
void onFailure(Exception e); // 下载失败(子线程回调)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user