Android: Storing Image into project directory (files)?

…衆ロ難τιáo~ 提交于 2019-12-12 09:41:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4336865/android-storing-image-into-project-directory-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!