我正在尝试保存主题,我在drwable文件夹里有主题(图片)。我有主题(图像)在drwable文件夹中。我正在显示图像的列表和点击相同的,我想保存选定的drawable资源在sharedpreferences,并从sharedpreferences得到相同的。
要做到这一点,我想将drawable资源转换成一个uri,并将uri转换为字符串。
我试着将drawable转换为像下面这样的uri。
public static String getURLForResource (int resourceId,Context context) {
//use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
/* return Uri
.parse("android.resource://"+ BuildConfig.APPLICATION_ID +
"/" +resourceId).toString();*/
Resources resources = context.getResources();
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ resources.getResourcePackageName(resourceId) + '/'
+ resources.getResourceTypeName(resourceId) + '/'
+ resources.getResourceEntryName(resourceId)).toString();
}
并从sharedprefences中重新获得这个uri字符串,并试图将其转换为位图。
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap =
BitmapFactory.decodeFile(sharedPreferencesData.getStr(
"ThemeName"),
options);*/
/* Uri myUri = Uri.parse(sharedPreferencesData.getStr(
"ThemeName"));
*/
/* Uri uri = Uri.parse(sharedPreferencesData.getStr("ThemeName"));
ContentResolver res = getContentResolver();
InputStream in = null;
try {
in = res.openInputStream(uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap artwork = BitmapFactory.decodeStream(in);*/
try {
Uri uri = Uri.parse(sharedPreferencesData.getStr("ThemeName"));
Bitmap bitmap =
MediaStore.Images.Media
.getBitmap(this.getContentResolver(), uri);
/*
InputStream stream =
getAssets().open(sharedPreferencesData.getStr(
"ThemeName"));
Drawable d = Drawable.createFromStream(stream, null);
URL url_value = new URL(sharedPreferencesData.getStr(
"ThemeName").trim());
Bitmap mIcon1 =
BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
*/
我尝试了多种方法,但都不成功。要么位图是空的,要么我得到的文件未找到异常和malformedException。
请帮助与相同的。
EDIT :
我从getURLForResource中得到如下字符串。
DImageUri: android.resource:/com.dailyfaithapp.dailyfaithdrawabletheme0。
我已经创建了一个班级,名字,字体等......我设置了相同的值,如。
public void setThemes(){
Themes themes = new Themes();
themes.setId(1);
themes.setImage(Utils.getURLForResource(R.drawable.theme1,this));
themes.setFont("AlexBrush-Regular.ttf");
themesArrayList.add(themes);
themes = new Themes();
themes.setId(2);
themes.setImage(Utils.getURLForResource(R.drawable.theme2,this));
themes.setFont("SkinnyJeans.ttf");
themesArrayList.add(themes);
themes = new Themes();
themes.setId(3);
themes.setImage(Utils.getURLForResource(R.drawable.theme3,this));
themes.setFont("Roboto-Thin.ttf");
themesArrayList.add(themes);
themes = new Themes();
themes.setId(4);
themes.setImage(Utils.getURLForResource(R.drawable.theme4,this));
themes.setFont("Raleway-Light.ttf");
themesArrayList.add(themes);
}
你可以尿到drawable
public static Drawable uriToDrawable(Uri uri) {
Drawable d = null;
try {
InputStream inputStream;
inputStream = G.context.getContentResolver().openInputStream(uri);
d = Drawable.createFromStream(inputStream, uri.toString());
} catch (FileNotFoundException e) {
d = G.context.getResources().getDrawable(R.drawable.ic_launcher_background);
}
return d;
}
或者做这些。
将位图保存在应用根目录下,然后再次读取
private void saveBitmap(Bitmap bitmap){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/images
File directory = cw.getDir("images", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"bg.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {}
}
读取位图
private Bitmap readBitmap(String path)
{
try {
File f=new File(path, "bg.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
return b;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}