将图库中的图像分别加载到图像视图中

Loading Images from Gallery into ImageViews Separatly

好的,所以我有 3 个 ImageView,当我单击它们时,它们会从图库中加载图像。

我的问题是,如何设置我在特定 ImageView 中加载的图像。

我遇到了 ActivityForResult 代码的一些问题(例如,当加载图像并将其设置在 ImageView1 中时,所有 ImageView 立即获取相同的图像)

完整代码:

package kane.navigationdrawer.activity;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import kane.navigationdrawer.R;
import static android.R.attr.data;
import static kane.navigationdrawer.R.id.spinner;
import static kane.navigationdrawer.R.id.spinner4;

   public class FormActivity extends AppCompatActivity {

private Spinner spinner1;
Button b1;
ImageView img1,img2,img3;
private static int RESULT_LOAD_IMAGE = 1;
private static int RESULT_LOAD_IMAGE2 = 1;
private static int RESULT_LOAD_IMAGE3 = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form);

    Spinner spinner = (Spinner) findViewById(R.id.spinner4);
    Spinner spinner2 = (Spinner) findViewById(R.id.spinner);
    img1 = (ImageView) findViewById(R.id.img1);
    img2 = (ImageView) findViewById(R.id.img2);
    img3 = (ImageView) findViewById(R.id.img3);


    spinner2.setAdapter(
            new ArrayAdapter<CharSequence>(this,
                    R.layout.custom_spinner_item,
                    FormActivity.this.getResources().getTextArray(R.array.cat_arrays)));


    spinner.setAdapter(
            new ArrayAdapter<CharSequence>(this,
                    R.layout.custom_spinner_item,
                    FormActivity.this.getResources().getTextArray(R.array.vil_arrays)));

    img1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent loadIntent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(loadIntent, RESULT_LOAD_IMAGE);
        }
    });


    img2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent loadIntent2 = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(loadIntent2, RESULT_LOAD_IMAGE2);
        }
    });

    img3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent loadIntent3 = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(loadIntent3, RESULT_LOAD_IMAGE3);
        }
    });
}






@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        img1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }

    if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        img2.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
    if (requestCode == RESULT_LOAD_IMAGE3 && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        img3.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}
}

需要你的帮助,谢谢

您定义了三个请求代码,但它们都具有相同的值,因此无论您选择何种图像,都会执行所有三个 if 语句。

只需为您的请求代码变量分配不同的值即可解决问题。

private static int RESULT_LOAD_IMAGE = 1;
private static int RESULT_LOAD_IMAGE2 = 2;
private static int RESULT_LOAD_IMAGE3 = 3;