我有两项活动。第一个活动是用于上传图像的布局。第二个活动是适配器,并显示上传的图像库。我想在Adapter中编写一个内部类onImageClickListener来选择上传图像。
class OnImageClickListener implements View.OnClickListener {
int _position;
//constructor
public OnImageClickListener (int position) {
_position = position;
}
@Override
public void onClick(View v) {
Intent i = new Intent(_activity, LayoutForThreeActivity.class);
}
}
clicklistener必须从LayoutForThreeActivity上的库中提交所选图像,并将其显示到imageView中。我怎么能这样做?
以下是LayoutForThreeActivity
中的代码 image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LayoutForThreeActivity.this, GridViewActivity.class);
startActivityForResult(intent, RESULT_LOAD_IMAGE1);
//Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//startActivityForResult(intent, RESULT_LOAD_IMAGE1);
}
});
image2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE2);
}
});
image3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE3);
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LayoutForThreeActivity.this, SaveCompositionActivity.class);
startActivity(intent);
//Toast.makeText(getApplicationContext(), "Save", Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK && null != data) {
//Todo
}
if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK && null != data) {
//Todo
}
if (requestCode == RESULT_LOAD_IMAGE3 && resultCode == RESULT_OK && null != data) {
//Todo
}
}
答案 0 :(得分:0)
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imgUpload.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
公共类MainActivity扩展了AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 1;
private ImageView imgUpload;
private Button btnUpload;
private Uri filePath;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgUpload = (ImageView) findViewById(R.id.imgUpload);
btnUpload = (Button) findViewById(R.id.btnUpload);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imgUpload.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
} }