Android:将图像存储到项目目录(文件)中?

发布于 2024-10-05 11:10:46 字数 53 浏览 0 评论 0原文

我想将我的位图图像存储到项目目录中。我如何访问我的项目文件夹或者我的项目文件夹的地址是什么?

I want to store my Bitmap image into project directory. How can I have access to my project folder or what is the address of my project folder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2024-10-12 11:10:46

您必须将图像放入 res/drawable 文件夹中。然后,您可以使用以下方式访问它们:R.drawable.name_of_image(对于 name_of_image.pngname_of_image.jpg)。

如果您想通过原始名称访问它们,最好将它们保存在 assets 文件夹中。然后,您可以使用 AssetManager 访问它们:

AssetManager am = getResources().getAssets();
try {
    InputStream is = am.open("image.png");
    // use the input stream as you want
} catch (IOException e) {
    e.printStackTrace();
}

如果您想保存以编程方式创建的图像,您可以执行以下操作:

try {
       FileOutputStream out = new FileOutputStream(context.getFilesDir().getAbsolutePath()+"/imagename.png");
       bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
       e.printStackTrace();
}

您无法将其保存到项目目录中。我建议你阅读有关 android 包如何工作的文档,因为你似乎不明白。

You have to put the images in the res/drawable folder. Then, you can access them by using: R.drawable.name_of_image (for name_of_image.png or name_of_image.jpg).

If you want to access them by their original name, you better save them in the assets folder. Then, you can access them by using the AssetManager:

AssetManager am = getResources().getAssets();
try {
    InputStream is = am.open("image.png");
    // use the input stream as you want
} catch (IOException e) {
    e.printStackTrace();
}

If you want to save a programatically created image, you can do:

try {
       FileOutputStream out = new FileOutputStream(context.getFilesDir().getAbsolutePath()+"/imagename.png");
       bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
       e.printStackTrace();
}

You can't save it to your project directory. I recommend you to read the documentation about how android packages work, because it seems you don't understand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文