Android的:如何在ImageView的显示所选的图片库?所选、图片库、如何在、Android

2023-09-06 10:00:37 作者:渣男研究所

我遵循这个指令http://www.$c$crzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/.

但我有这个问题...

But I'm having this problem...

从图库中选择图片是不是在我的ImageView显示。

The selected image from gallery is not displaying in my imageview.

这是我的code布局:

this is my code for layout:

<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" />

之类的code:

@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);
    }
}

我不知道什么是错我的code。

I don't know what is wrong with my code.

谁能帮我?

推荐答案

如果图像尺寸很大,你可以按比例缩小的位图,如下

If the image size is large you can scale down the bitmap as below

            BitmapFactory.Options options = new BitmapFactory.Options();

            options.inSampleSize = 2;
            bitmap = BitmapFactory.decodeFile(path, options);
            image.setImageBitmap(BitmapFactory.decodeFile(picturePath));

此外,你可以决定基于图像的大小和显示尺寸的采样大小的替代

Also as an alternative you can determine the sampleSize based on the image size and the display size