Merge pull request '修复了便签背景缺陷' (#20) from cuijiaxiang_branch into master

luhaozhe_branch
pr9ixgmc2 2 months ago
commit c6ebbeade6

@ -28,6 +28,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
@ -83,6 +84,8 @@ import net.micode.notes.widget.NoteWidgetProvider_2x;
import net.micode.notes.widget.NoteWidgetProvider_4x;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -218,13 +221,19 @@ public class NotesListActivity extends Activity implements OnClickListener, OnIt
// 处理从相册选择图片的结果
if (data != null && data.getData() != null) {
try {
// 保存图片路径
mCurrentBackgroundType = BACKGROUND_TYPE_ALBUM;
mCurrentBackgroundPath = data.getData().toString();
// 更新背景
updateBackground();
// 保存设置
saveBackgroundSetting();
// 将相册图片复制到应用内部存储
String internalPath = saveImageToInternalStorage(data.getData());
if (!TextUtils.isEmpty(internalPath)) {
// 保存内部存储路径
mCurrentBackgroundType = BACKGROUND_TYPE_ALBUM;
mCurrentBackgroundPath = internalPath;
// 更新背景
updateBackground();
// 保存设置
saveBackgroundSetting();
} else {
Toast.makeText(this, "图片保存失败", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "图片加载失败", Toast.LENGTH_SHORT).show();
@ -1640,8 +1649,17 @@ public class NotesListActivity extends Activity implements OnClickListener, OnIt
} else if (BACKGROUND_TYPE_ALBUM.equals(mCurrentBackgroundType) && !TextUtils.isEmpty(mCurrentBackgroundPath)) {
// 使用相册图片作为背景
try {
android.graphics.Bitmap bitmap = android.graphics.BitmapFactory.decodeStream(
getContentResolver().openInputStream(android.net.Uri.parse(mCurrentBackgroundPath)));
android.graphics.Bitmap bitmap = null;
// 检查路径类型
if (mCurrentBackgroundPath.startsWith("content://")) {
// 如果是URI字符串
bitmap = android.graphics.BitmapFactory.decodeStream(
getContentResolver().openInputStream(android.net.Uri.parse(mCurrentBackgroundPath)));
} else {
// 如果是内部存储路径
bitmap = android.graphics.BitmapFactory.decodeFile(mCurrentBackgroundPath);
}
if (bitmap != null) {
// 计算屏幕尺寸
android.util.DisplayMetrics displayMetrics = new android.util.DisplayMetrics();
@ -1750,4 +1768,42 @@ public class NotesListActivity extends Activity implements OnClickListener, OnIt
}
});
}
/**
*
* @param uri URI
* @return
*/
private String saveImageToInternalStorage(Uri uri) {
try {
// 创建内部存储目录
File directory = new File(getFilesDir(), "backgrounds");
if (!directory.exists()) {
directory.mkdirs();
}
// 创建输出文件
String fileName = "background_" + System.currentTimeMillis() + ".jpg";
File outputFile = new File(directory, fileName);
// 复制图片
InputStream inputStream = getContentResolver().openInputStream(uri);
FileOutputStream outputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
// 返回文件路径
return outputFile.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

Loading…
Cancel
Save