gpt4 book ai didi

android - 我如何从图库中选择图像?

转载 作者:行者123 更新时间:2023-11-29 16:02:18 27 4
gpt4 key购买 nike

我正在制作一个项目,其中我使用两个 ImageView,我想从 gallery 中选择两个不同的图像,我想设置两个不同的图像ImageViews。我可以选择一个并设置在一个 imageview 上。但我不明白我如何在第二个 imageview 案例中做到这一点?

我使用的代码是...

ImageView iv1,iv2;

private static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);


setContentView(R.layout.layout2);

iv1=(ImageView)findViewById(R.id.imageView21);
iv1.setOnClickListener(this);
iv2=(ImageView)findViewById(R.id.imageView21);
iv2.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

if(v.getId()==R.id.imageView21){
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
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();


iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}


}

更新 1...

这是一个问题,它正在两个 imageview 上设置图像,即使我正在尝试为一个...

public class LayoutDisplay2 extends Activity{

ImageView iv1, iv2;
private static int RESULT_LOAD_IMAGE1 = 1;
private static int RESULT_LOAD_IMAGE2 = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);


setContentView(R.layout.layout2);

iv1 = (ImageView) findViewById(R.id.imageView21);
iv1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE1);
}
});
iv2 = (ImageView) findViewById(R.id.imageView22);
iv2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

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

startActivityForResult(in, RESULT_LOAD_IMAGE2);
}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE1 && 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();

iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}
if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Toast.makeText(getApplicationContext(), "in second",
Toast.LENGTH_SHORT).show();
Log.i("Second", "in second");
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

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

iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}

}

}

最佳答案

尝试如下:

ImageView iv1, iv2;
private static int RESULT_LOAD_IMAGE1 = 1;
private static int RESULT_LOAD_IMAGE2 = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.layout2);

iv1 = (ImageView) findViewById(R.id.imageView21);
iv1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE1);
}
});
iv2 = (ImageView) findViewById(R.id.imageView22);
iv2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

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

startActivityForResult(in, RESULT_LOAD_IMAGE2);
}
});


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE1 && 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();

iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}
if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Toast.makeText(getApplicationContext(), "in second",
Toast.LENGTH_SHORT).show();
Log.i("Second", "in second");
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

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

iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}

}

关于android - 我如何从图库中选择图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103792/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com