package mydemo.mycom.demo2; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class ImagePrev extends ActionBarActivity implements View.OnClickListener { private ImageView iv; private Button btn_prev_image; private EditText et_image_path; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_prev); iv = (ImageView)findViewById(R.id.iv); btn_prev_image = (Button)findViewById(R.id.btn_prev_image); et_image_path = (EditText)findViewById(R.id.et_image_path); btn_prev_image.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_image_prev, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_prev_image: String path = et_image_path.getText().toString().trim(); if(TextUtils.isEmpty(path)) { Toast.makeText(this,"图片路径不能",Toast.LENGTH_SHORT).show(); return; } try { //浏览图片 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if(code==200) { InputStream is = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); iv.setImageBitmap(bitmap); } else { Toast.makeText(this,"图片浏览失败",Toast.LENGTH_SHORT).show(); } }catch (Exception e) { Toast.makeText(this,"图片浏览失败",Toast.LENGTH_SHORT).show(); e.printStackTrace(); } break; } } }
Activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="mydemo.mycom.demo2.ImagePrev"> <ImageView android:layout_weight="1000" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/iv"/> <EditText android:text="http://192.168.1.1:91/Resource/TopicInfo/2015-05-23/130768288778069472.png" android:hint="图片路径" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/et_image_path"/> <Button android:text="浏览" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/btn_prev_image"/> </LinearLayout>
时间: 2024-10-24 15:59:06