Ways to Upload an Image to Android

1. Method 1 (The simplest I think)

- Make sure to put your gclue.png in the drawable directory!




2. Using FileOutput Stream

To read an image file that is packaged with your software, you should put it under the res/drawable folder relative to your software's root.

Once the image is in the folder, a resource ID will be generated automatically when you re-compile the package. For example, you have the image file called pic1.jpg. It will become accessible programmatically through its resource ID R.drawable.pic1. You can see that the image file extension has been stripped off and the upper case R represents the overall resource file, R.java. R.java is generated automatically and should not be edited unless you have a pretty good understanding of how resources are structured in this file. Here is the
code segment showing how you can refer to an image through its resource ID.






To read or write an image file without specifying the folder structure, it will be under /data/data/YourPackageName/files/ on the emulator. For example, you will create your package name for your example as package com.cyl.TutorialOnImages. Therefore, if you create a new image file at runtime, it will be under the /data/data/com.cyl.TutorialOnImages/files/ folder. Please note that each Android application will start with its own user and group ID, so some file folders are not accessible through your software unless they are specifically set to do so. Here is the code for when you want to output a bitmap onto a file called output.jpg.






3. Directly in main.xml (but you need to find a way to upload the image in the emulator
or android phone for this to work. You can use the code in number 2 to upload the picture.



That's it!

コメント

人気の投稿