adv_sdcard_image_upload.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">
<TextView android:id="@+id/txtv_title" android:text="本地400電話 圖片添加" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:gravity="center" android:layout_weight="0.29">
<Button android:text="選擇" android:id="@+id/adv_btn_xuanze" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="提交" android:id="@+id/adv_btn_tijiao" android:layout_marginLeft="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="返回" android:id="@+id/adv_btn_fanhui" android:layout_marginLeft="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_weight="0.22">
<TextView android:text="任務名稱:" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/editText1">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<ImageView android:layout_gravity="center_horizontal" android:src="@drawable/clickbutttonstyle"
android:id="@+id/adv_img_show" android:layout_width="302dp" android:layout_height="284dp"></ImageView>
</LinearLayout>
adv_sdcard_image_upload.java
package adv.activity;
import java.io.FileNotFoundException;
import frame.hrxcCall.R;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class adv_sdcard_image_upload extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adv_sdcard_image_upload);
init();
}
/**
* 初始化方法 http://www.my400800.cn
*/
private void init() {
// 事件註冊start
// 選擇
Button adv_btn_xuanze = (Button) findViewById(R.id.adv_btn_xuanze);
// 提交
Button adv_btn_tijiao = (Button) findViewById(R.id.adv_btn_tijiao);
// 返回
Button adv_btn_fanhui = (Button) findViewById(R.id.adv_btn_fanhui);
// 返回按鈕按下處理事件
adv_btn_fanhui.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
finish();// 這個是關鍵
}
});
// 事件註冊end
adv_btn_xuanze.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
/* 開啟Pictures畫面Type設定為image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT這個Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片後返回本畫面 */
startActivityForResult(intent, 1);
}
});
// /* 開啟Pictures畫面Type設定為image */
// intent.setType("image/*");
// /* 使用Intent.ACTION_GET_CONTENT這個Action */
// intent.setAction(Intent.ACTION_GET_CONTENT);
// /* 取得相片後返回本畫面 */
// startActivityForResult(intent, 1);
}
/**
* 檔案選取完成回呼函數
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.e("uri", uri.toString());
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
ImageView adv_img_show = (ImageView) findViewById(R.id.adv_img_show);
/* 將Bitmap設定到ImageView */
adv_img_show.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
Toast.makeText(getApplicationContext(), "選擇檔案沒有發現", Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
效果: