2011年10月2日星期日

從 SD Card 中讀取圖像檔案

從 SD Card 中讀取圖像檔案

package com.AndroidLoadFromSD;

import java.io.File;

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

public class AndroidLoadFromSD extends Activity {

ImageView Image;
Button LoadButton;
Bitmap bmImage;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Image = (ImageView)findViewById(R.id.image);
LoadButton = (Button)findViewById(R.id.load);

LoadButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String extStorage = Environment.getExternalStorageDirectory().toString();
String file = new File(extStorage, "myFile.PNG").toString();
Bitmap bm = BitmapFactory.decodeFile(file);
Image.setImageBitmap(bm);

Toast.makeText(AndroidLoadFromSD.this,
extStorage+"/myFile.PNG",
Toast.LENGTH_LONG).show();

}});
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/load"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Image (myFile.PNG)"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


相關文章:
- 儲存圖像到 SD Card 中



2 則留言:

  1. public void onClick(View v) {

    Toast.makeText(AndroidLoadFromSD.this,extStorage+"/myFile.PNG",

    輸入兩行發生錯誤耶

    回覆刪除
    回覆
    1. 更正:
      當輸入:"public void onClick(View v) {"的時候,會出現以下錯誤訊息

      The method onClick(View) of type new View.OnClickListener(){} must override a superclass method

      刪除