PowenKo, Android, Tutorial 013-1, UI, ImageView, display image from local file.

August 29, 2011   ·   0 Comments

package com.powenko.Tutorial_UI_Image_getLocalFileDrawable;

import java.io.File;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Tutorial_UI_Image_getLocalFileDrawableActivityActivity extends Activity
{

  private ImageView mImageView;
  private Button mButton;
  private TextView mTextView;
  private String fileName="/data/data/Tutorial_UI_Image_getLocalFileDrawableActivityActivity/001.png";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    /* ∏¸§Jmain.xml Layout */
    setContentView(R.layout.main);

    mButton = (Button)findViewById(R.id.mButton);
    mButton.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {

        mImageView = (ImageView)findViewById(R.id.mImageView);
        mTextView=(TextView)findViewById(R.id.mTextView);

        File f=new File(fileName);
        if(f.exists())
        {

          Bitmap bm = BitmapFactory.decodeFile(fileName);
          mImageView.setImageBitmap(bm);
          mTextView.setText(fileName);
        }
        else
        {
          mTextView.setText("Cannot find the file");
        }
      }
    });
  }
}

res\layout\main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
  android:id="@+id/layout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"

  xmlns:android="http://schemas.android.com/apk/res/android"
>
  <TextView
    android:id="@+id/mTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:layout_x="30px"
    android:layout_y="30px"
  />
  <ImageView
    android:id="@+id/mImageView"
    android:layout_width="250px"
    android:layout_height="188px"
    android:src="@drawable/icon"
    android:layout_x="30px"
    android:layout_y="62px"
  />
  <Button
    android:id="@+id/mButton"
    android:layout_width="155px"
    android:layout_height="wrap_content"
    android:text="change image"
    android:textSize="18sp"
    android:layout_x="80px"
    android:layout_y="302px"
  />
</AbsoluteLayout>

sample code:
Tutorial_UI_Image_getLocalFileDrawable

solution 2:


	Bitmap  mIcon1 = BitmapFactory.decodeResource(this.getResources(),R.drawable.icon);
	  ImageView      icon = (ImageView) this.findViewById(R.id.widget28);
	        icon.setImageBitmap(mIcon1);

// mIcon1=BitmapFactory.decodeResource(convertView.getResources(),R.drawable.icon);
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon