相关疑难解决方法(0)

如何在Android中将Drawable转换为int,反之亦然

我想将Drawable转换为int然后反之亦然.基本上我想将Arraylist对象保存到sharedPrefrence中.为此我有实施Gson到Srtring转换方法.如果我在这里使用Drawable而不是int,那么Gson String转换需要很多时间.所以我想使用int而不是Drawable.

 private List<AppInfo> apps = null;

   public void setIcon(int icon) {
    this.icon = icon;
}

   apps.get(position).setIcon(mContext.getResources().getDrawable(apps.get(position).getIcon()));
Run Code Online (Sandbox Code Playgroud)

AppInfo在哪里

    public AppInfo(String appname, String pname, String versionName, int versionCode, int icon, int color) {
    this.appname = appname;
    this.pname = pname;
    this.versionName = versionName;
    this.versionCode = versionCode;
    this.icon = icon;
    this.color = color;
}
Run Code Online (Sandbox Code Playgroud)

以下是将Custom对象的ArrayList转换为String的源代码,以便我可以将其保存到SharedPrefrence.

            Gson gson = new Gson();
            apps.get(number).setColor(picker.getColor());
            String JsonAppsdata = gson.toJson(apps);
            System.out.println("Storing="+JsonAppsdata);
            utility.StoreData(getApplicationContext(), JsonAppsdata);
Run Code Online (Sandbox Code Playgroud)

android drawable android-sharedpreferences

7
推荐指数
1
解决办法
2万
查看次数

在Android上序列化Drawable对象

我正在尝试通过缓存图像来加速我的ListView,并在滚动列表时从手机而不是互联网加载它们.但是,当我尝试序列化Drawable对象时,我遇到了异常.这是我的功能:

    private void cacheImage(Drawable dr, Article a){
    FileOutputStream fos;
    try {
        fos = openFileOutput(a.getArticleId().toString(), Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(dr); 
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

这段漂亮的代码导致:

java.io.NotSerializableException:android.graphics.drawable.BitmapDrawable

序列化这些图像的最佳方法是什么?

serialization android drawable

6
推荐指数
1
解决办法
4957
查看次数

将位图添加到可绘制资源

如何Bitmap从网络(在我的意思是应用程序!)中添加一个可绘制资源?所以我可以访问Bitmap使用R.drawable.bitmapName...

android android-resources

5
推荐指数
1
解决办法
1万
查看次数

Android,如何在内部存储中存储图像?

我想在内部存储(而不是外部存储)上存储位图图像.我写了这段代码,但似乎有问题.因为当我从DDMS下载图像时,我无法打开它.

public String writeFileToInternalStorage(Context context, Bitmap outputImage) {

        String fileName = Long.toString(System.currentTimeMillis()) + ".png";

        try {
            OutputStreamWriter osw = new OutputStreamWriter(context.openFileOutput(fileName, Context.MODE_PRIVATE));
            osw.write(outputImage.toString());
            Log.i(TAG, "Image stored at: " + fileName);
        } catch (Exception e) {
            Log.w(TAG, e.toString()); 
            fileName = null;
        } 

        return fileName;
    } 
Run Code Online (Sandbox Code Playgroud)

android bitmap

3
推荐指数
1
解决办法
7714
查看次数

如何在sdcard上保存位图图像jpeg?

我正在尝试编写应用程序以从图库中获取图像,然后在其上执行一些效果,并在显示图像后,我想将其保存为SdCard中的JPG.任何人都可以告诉我如何在SD卡上保存图像,我应该在哪里放保存代码?这是我想要保存的地方,点击button2后:

public void onClick(View arg0) {
                    Bitmap yourimage;
                    yourimage=toGrayscale(yourSelectedImage);
                    ImageButton effected=(ImageButton)findViewById(R.id.imageButton2);
                    int width = 150;
                    int height =150; 
                    Bitmap resizedbitmap=Bitmap.createScaledBitmap(yourimage, width, height, true);
                    effected.setImageBitmap(resizedbitmap);
}
Run Code Online (Sandbox Code Playgroud)

我的意思是在设置图像为resizedbitmap后,我想将其保存在SD卡中

android jpeg image bitmap save

2
推荐指数
1
解决办法
8532
查看次数