但是我遇到了这个问题...
从图库中选择的图像未显示在我的图像视图中。
这是我的布局代码:
<RelativeLayout 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" >
<TextView
android:id="@+id/lbl_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="150dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lbl_name"
android:layout_below="@+id/lbl_name"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/lbl_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt_name"
android:layout_below="@+id/txt_name"
android:text="Description"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txt_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lbl_description"
android:layout_below="@+id/lbl_description"
android:ems="10"
android:inputType="textMultiLine"
android:minHeight="100dip"
android:maxHeight="100dip"
android:scrollbars="horizontal" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/lbl_clue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt_description"
android:layout_below="@+id/txt_description"
android:text="Clue"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txt_clue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lbl_clue"
android:layout_below="@+id/lbl_clue"
android:ems="10"
android:inputType="textMultiLine"
android:minHeight="50dip"
android:maxHeight="50dip"
android:scrollbars="horizontal" />
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/txt_clue"
android:layout_below="@+id/txt_clue"
android:text="Save" />
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_clue"
android:layout_toLeftOf="@+id/btn_submit"
android:text="Clear" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/txt_name"
android:layout_alignLeft="@+id/lbl_name"
android:maxHeight="80dip"
android:maxWidth="80dip"
android:src="@drawable/icon_game" />
<Button
android:id="@+id/btn_select"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_marginBottom="47dp"
android:layout_marginLeft="22dp"
android:layout_toRightOf="@+id/imageView1"
android:text="Select Image" />
类的代码:
@SuppressLint("NewApi")
public class NewDataActivity extends Activity {
final TestAdapter mDbHelper = new TestAdapter(this);
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_data);
getActionBar().setDisplayHomeAsUpEnabled(true);
final Button btn_submit = (Button) findViewById(R.id.btn_submit);
final Button btn_select = (Button) findViewById(R.id.btn_select);
final EditText txt_name = (EditText) findViewById(R.id.txt_name);
final EditText txt_desc = (EditText) findViewById(R.id.txt_description);
final EditText txt_clue = (EditText) findViewById(R.id.txt_clue);
img = (ImageView)findViewById(R.id.imageView1);
mDbHelper.createDatabase();
mDbHelper.open();
btn_submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isEmpty(txt_name) || isEmpty(txt_desc)){
if(isEmpty(txt_name)){
Toast.makeText(getApplicationContext(), "Name must not empty. ", Toast.LENGTH_SHORT).show();
}
else if(isEmpty(txt_desc)){
Toast.makeText(getApplicationContext(), "Description must not empty. ", Toast.LENGTH_SHORT).show();
}
} else {
mDbHelper.insertData(txt_name.getText().toString(), txt_desc.getText().toString(), txt_clue.getText().toString());
mDbHelper.close();
Intent myIntent = new Intent(v.getContext(), ManageCustom.class);
startActivityForResult(myIntent, 0);
}
}
});
btn_select.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_new_data, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private boolean isEmpty(EditText etText) {
return etText.getText().toString().trim().length() == 0;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
我不知道我的代码有什么问题。
谁能帮我?