android - 所选图库图像未显示在 ImageView 中

标签 android

必须在 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 中。任何人都可以帮助我解决这个问题。谢谢。

最佳答案

我已经使用了这段代码:

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();
            }


        }
    }

关于android - 所选图库图像未显示在 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37162382/

相关文章:

android - 在具有全屏 Activity 的 Android Lollipop 上设置导航栏颜色

android - Keystore 在 5 个月后过期

Android MapView 不显示缩放级别 1 的 map

android为方向提供不同的drawable资源

android - 如何在android中的单个 Activity 中处理多个 Intent ?

Android jetpack compose Material 3 无法对齐复选框文本

android - 在Android Eclipse IDE中单独重命名APP的名称

android-layout - TextView中的Android多行

android - 模拟器 : ERROR: Could not initialize OpenglES emulation, 使用 '-gpu off' 禁用它

java - 当我们暂停游戏时后台发生了什么?游戏如何恢复和开始?