TQC-205 命令模式(command) 存/放檔案
這是TQC的205題,
程式碼為自行撰寫,
僅供參考。
主要是練習在命令模式(command) 存/放檔案。
1-開啟模擬器
2-切換到模擬器目錄
3-下「adb push 」-放檔案
程式部份注意用檔案放置圖片。
File f=new File(fileName);
if(f.exists())
{
mTextView.setText(fileName);
Bitmap bm = BitmapFactory.decodeFile(fileName);
mImageView.setImageBitmap(bm);
}
else
{
mTextView.setText("檔案不存在");
}
package COM.TQC.GDD02;
import java.io.File;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class GDD02 extends Activity
{
private ImageView mImageView;
private Button mButton,mButton2;
private TextView mTextView;
//private String fileName="/data/data/COM.TQC.GDD02/001.PNG";
private String fileName= "/sdcard/png002.PNG";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
setlistener();
mImageView.setImageResource(R.drawable.icon);
//mTextView.setText(mImageView.);
}
public void findview()
{
mImageView = (ImageView)findViewById(R.id.mImageView);
mTextView=(TextView)findViewById(R.id.mTextView);
mButton = (Button)findViewById(R.id.mButton);
mButton2=(Button)findViewById(R.id.button1);
}
public void setlistener()
{
mButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
//mImageView.setImageResource(R.drawable.png002);
File f=new File(fileName);
if(f.exists())
{
mTextView.setText(fileName);
Bitmap bm = BitmapFactory.decodeFile(fileName);
mImageView.setImageBitmap(bm);
}
else
{
mTextView.setText("檔案不存在");
}
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
mTextView.setText("");
mImageView.setImageResource(R.drawable.icon);
}
});
}
}
自我LV~