Android: Unable to display png image in an ImageView
I am writing an Android app that includes an ImageView. The image to be included in the view resides on the local app directory of the emulator. The image is not being displayed in the view. The relavent XML and method used to display the image are given below.
The image file is a "png" format file and is able to be opened from the local app directory. The path returned by getFilesDir() includes the full path, "/data/data/net.vitalrecord.android/files." This path designation does not exist on the development system but does exists when DDMS File Explorer is used.
Any help with this problem is开发者_如何学Python greatly appreciated.
Thanks.
LAYOUT XML:
<AbsoluteLayout>
......
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="100px"/>
</AbsoluteLayout>
Method: public void checkFilesExists() {
.....
try {
Context context = getApplicationContext();
FILENAME = getString(R.string.MedicalImage);
fis = openFileInput(FILENAME);
int size = fis.available();
byte[] buffer = new byte[size];
fis.read(buffer);
fis.close();
setContentView(R.layout.medtogoimage);
ImageView iv = (ImageView)findViewById(R.id.picture);
String imagePath = context.getFilesDir() + "/" + FILENAME;
Drawable d = Drawable.createFromPath(imagePath);
iv.setImageDrawable(d);
medtogo.setbMedicalImageFileExists(true);
} catch (IOException e) {
medtogo.setbMedicalImageFileExists(false);
}
You should put images inside the res/drawable folder. Then you can get its reference with the R class, or assign it to the ImageView from layout xml file.
Did you try setting the mode to MODE_WORLD_READABLE?
精彩评论