我的android应用程序在我选择图片的那一刻就挂了,没有异常,我不明白为什么,请帮助下面的代码
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class SelectPhoto extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photopick_layout);
Button btn=(Button)findViewById(R.id.clickme);
btn.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
TextView textTargetUri = (TextView)findViewById(R.id.targeturi);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
textTargetUri.setText(targetUri.toString());
Intent mi = new Intent(this , ImgDisplay.class);
mi.putExtra(getString(R.string.app_name),targetUri);
startActivity(mi);
}
}
}
Imagedisplay Activity这是它应该从其 uri 中显示所选图像的部分
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
public class ImgDisplay extends Activity {
private String a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_display);
a =getIntent().getStringExtra(getString(R.string.app_name));
Uri myUri = Uri.parse(a);
ImageView b=(ImageView) findViewById(R.id.image_view);
Bitmap myBitmap = BitmapFactory.decodeFile(a);
b.setImageBitmap(BitmapFactory.decodeFile(a));
b.setImageBitmap(myBitmap);
}
}
照片选择布局这是第一个要求您从图库中选择图像的布局
<?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:background="#00AEC5">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/btn2"
>
<TextView
android:id="@+id/targeturi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/clickme"
android:text="@string/pick"
style="@style/style1"
android:layout_centerInParent="true"
></Button>
</RelativeLayout>
</LinearLayout>
**Image_display layout* 这是显示图像的布局
<?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:background="#00AEC5">
<ImageView android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="42dp"
android:maxHeight="42dp"
android:scaleType="fitCenter"
android:layout_marginLeft="3dp"
></ImageView>
</LinearLayout>
我敢肯定这很容易,但我是新手