我有全局变量 byte [] byteArr; ,我想在单击图像时保存bytearray的值,并在 TakeImage(img_view); 方法中设置该值 ed [finalI] .setText(bytearray); 下面的代码在onCreate上面
private Bitmap global_bitmap;
下面的代码位于 onCreate 中,当我单击它时,它会弹出选择画廊的选项
final XEditText ed[] = new XEditText[arraylist.size()];
final XImageView xImageViews[] = new XImageView[arraylist.size()];
for (int i = 0; i < arraylist.size(); i++) {
ed[i] = new XEditText(this);
xImageViews[i] = new XImageView(this);
ed[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TakeImage(img_view);
xImageViews[finalI1].setImageBitmap(global_bitmap);
ed[finalI1].setText(global_bitmap.toString());
}
});
}
这是我从oncreate调用的方法
public void TakeImage(final ImageView imageview) {
AlertDialog.Builder builderimage = new AlertDialog.Builder(JSON_Form_Activity.this);
builderimage.setTitle("Choose Image");
builderimage.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
builderimage.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builderimage.show();
}
在这之后,有如下所示的onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri uri = data.getData();
try {
img_view.setVisibility(View.VISIBLE);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArr = stream.toByteArray();
Path_Image_model path_image_model = new Path_Image_model();
path_image_model.setPath(byteArr.toString());
Log.d(TAG, "===byteArr_PickImage==" + byteArr);
img_view.setImageBitmap(bitmap);
global_bitmap=bitmap;
resizedFile = new File(uri.getPath());
Log.d(TAG, "===FileImage==" + resizedFile);
} catch (IOException e) {
e.printStackTrace();
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
第一次显示它为空值,第二次显示一个以前的值,我希望从img_btn.setOnClickListener单击库中选择的imageview的真实global_bitmap值
答案 0 :(得分:0)
onActivityResultMethod:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri uri = data.getData();
try {
img_view.setVisibility(View.VISIBLE);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArr = stream.toByteArray();
Path_Image_model path_image_model = new Path_Image_model();
path_image_model.setPath(byteArr.toString());
Log.d(TAG, "===byteArr_PickImage==" + byteArr);
img_view.setImageBitmap(bitmap);
ed[finalI].setText(byteArr);
resizedFile = new File(uri.getPath());
Log.d(TAG, "===FileImage==" + resizedFile);
} catch (IOException e) {
e.printStackTrace();
}
}
请向您更新onActivityResultMethod