我必须在imageview中设置它后选择了图像。但是它没有在imageview中设置它。
我已发布相关代码。
EditViewProfileActivity.java
public class EditViewProfileActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private static final int CAMERA_PICTURE = 1;
private static final int GALLERY_PICTURE = 2;
File destination = null;
String filePath = null;
Bitmap chosenImage;
String imgSelected = null;
ImageView dateBirthImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_view_profile_activity);
galImageView = (ImageView) findViewById(R.id.gallery_image_edit_view);
galImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choosePictureAction();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICTURE
&& resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
destination = new File(selectedImagePath);
filePath = selectedImagePath;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
chosenImage = BitmapFactory.decodeFile(selectedImagePath, options);
if (chosenImage != null) {
imgSelected = "fulFilled";
galImageView.setImageBitmap(chosenImage);
Log.e("ChosenImage", "" + chosenImage);
}
} else if (requestCode == GALLERY_PICTURE
&& resultCode == RESULT_CANCELED) {
}
}
private void choosePictureAction() {
final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(EditViewProfileActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (items[which].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PICTURE);
} else if (items[which].equals("Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_PICTURE);
} else if (items[which].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
}
我不知道为什么选择的图片没有在imageview中显示。任何人都可以帮助我。谢谢。
答案 0 :(得分:1)
如果您只想在imageview中设置图像。你可以直接使用uri。喜欢这个
String stringUri = selectedImageUri.getPath();
galImageView.setImageURI(null);
galImageView.setImageURI(Uri.parse(stringUri));
答案 1 :(得分:1)
我使用过这段代码:
private void openGallery() {
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
try {
bitmaps = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmaps.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] byteArray = stream.toByteArray();
encodeded = Base64.encodeToString(byteArray, Base64.DEFAULT);
byte[] decodedString = Base64.decode(encodeded, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
userinfoimage.setImageBitmap(bitmaps);
new UploadImage().execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}