gpt4 book ai didi

android-如何将 View 保存到SD卡

转载 作者:行者123 更新时间:2023-11-29 00:12:34 25 4
gpt4 key购买 nike

我有一个 imageview ,我正在尝试通过这种方法从 imageview 保存位图

bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();

保存图像的 rgb 不像它在运行的应用程序中看起来的那样,所以我想知道是否有任何方法可以将 ImageView 直接保存到 sd 卡而不是获取位图然后将其保存到 sd 卡。请帮助我,我已经尝试了一切。

最佳答案

您可以使用以下代码读写对象:

public static void witeObjectToFile(Context context, Object object, String filename)
{
ObjectOutputStream objectOut = null;
try
{
FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE);
objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(object);
fileOut.getFD().sync();

} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (objectOut != null)
{
try
{
objectOut.close();
} catch (IOException e)
{
// do nowt
}
}
}
}

public static Object readObjectFromFile(Context context, String filename)
{
ObjectInputStream objectIn = null;
Object object = null;
try
{
FileInputStream fileIn = context.getApplicationContext().openFileInput(filename);
objectIn = new ObjectInputStream(fileIn);
object = objectIn.readObject();

} catch (FileNotFoundException e)
{
// Do nothing
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} finally
{
if (objectIn != null)
{
try
{
objectIn.close();
} catch (IOException e)
{
// do nowt
}
}
}

return object;
}

例如ArrayList可以保存为:

ImageView abcImage = (ImageView) readObjectFromFile(context, AppConstants.FILE_PATH_TO_DATA);

并写成:

witeObjectToFile(context, abcImage, AppConstants.FILE_PATH_TO_DATA);

关于android-如何将 View 保存到SD卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29166515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com