I want to click on an image and decide to get the item if it is within a certain range. I implemented it by referring to the following site.
http://www110.kir.jp/Android/ch0505.html
try is the basic operation method to handle ** exception ** in java.
try{
Processing that may throw an exception
}
catch(Exception class type Argument name){
Exception handling(Exception handler)
}
finally {
The last process that must be executed//Can be executed without fainally
}
What is running in try this time is opening the file with Inputstream and passing it to mBitmap. The code in catch is executed when the specified file does not exist in the assets folder (explained below). In the code I was referring to, nothing was executed when an exception occurred, but by executing e.printStackTrace (), the cause is output to the standard output.
try {
InputStream is = getResources().getAssets().open("sirokuma.jpg ");
mBitmap = BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
A class that reads a binary file. is is named after the acronym.
A folder located inside \ AndroidStudioProjects \ Test_Application (app name) \ app \ src \ main. You can store multiple image files together. I'm not sure what's different from res / drawable.
If you put it here, you can open it with getResources (). GetAssets (). Open ("filename"). If you want to write multiple files such as reading multiple files, you can improve readability by collecting the getResources (). GetAssets () part as AssetManager. AssetManager assetManager = getResources().getAssets(); assetManager.open ("file 1"); assetManager.open ("file 2");
Select the main file and right-click / New / Folder / Assets Folder to create it.
_ Referenced site _ http://pentan.info/android/app/sample/asset_manager.html
Recommended Posts