这里用到一个包 xutilslibrary.jar
直接上代码:
acricity_mai1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="150dp" /> <Button android:id="@+id/button" android:layout_width="100dp" android:layout_height="40dp" android:background="#1eb2ff" android:layout_gravity="center_horizontal" android:text="add_img" android:textColor="#ffffff" android:gravity="center"/> <Button android:id="@+id/fashe" android:layout_width="100dp" android:layout_height="40dp" android:background="#1eb2ff" android:textColor="#ffffff" android:text="发射" android:layout_gravity="center_horizontal" android:gravity="center" android:layout_marginTop="10dp"/> </LinearLayout>
package com.example.test1; import com.lidroid.xutils.BitmapUtils; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt=(Button) findViewById(R.id.button); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent picture=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(picture, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { final String pic_img; super.onActivityResult(requestCode, resultCode, data); if(requestCode==1){ Uri uri=data.getData(); String[] files={MediaStore.Images.Media.DATA}; Cursor c=this.getContentResolver().query(uri, files, null, null, null); c.moveToFirst(); int ii=c.getColumnIndex(files[0]); pic_img=c.getString(ii); c.close(); BitmapUtils bitmapUtils=new BitmapUtils(MainActivity.this); ImageView img=(ImageView) findViewById(R.id.image); bitmapUtils.display(img,pic_img ); Button bt1=(Button) findViewById(R.id.fashe); bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,MainActivity1.class); intent.putExtra("bian", pic_img); startActivity(intent); } }); } } }
package com.example.test1; import com.lidroid.xutils.BitmapUtils; import android.app.Activity; import android.os.Bundle; import android.widget.ImageView; public class MainActivity1 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.acricity_mai1); ImageView img=(ImageView) findViewById(R.id.img); String ui=getIntent().getStringExtra("bian"); BitmapUtils bitmapUtils=new BitmapUtils(MainActivity1.this); bitmapUtils.display(img, ui); } }权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />