setImageBitmap 不显示

raf*_*asq 4 android imageview android-layout

单击后,我的应用程序会在相机和图库之间进行选择,然后该图片将显示在 ImageView 中。我最初尝试显示完整图像,然后尝试使用位图方式,但没有任何效果。我只是得到一个空白的 ImageView。请给我一些关于我做错了什么的指导,并在必要时要求澄清:

相机/图库照片代码:

Uri outputFileUri;
private void openImageIntent() {
    // Determine Uri of camera image to save.
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
    root.mkdirs();
    final String fname = "img_" + System.currentTimeMillis() + ".jpg";
    final File sdImageMainDirectory = new File(root, fname);
    outputFileUri = Uri.fromFile(sdImageMainDirectory);
    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for(ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);
    }
    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
    startActivityForResult(chooserIntent, 0);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent returnIntent) {
    super.onActivityResult(resultCode, requestCode, returnIntent);
    if(requestCode == 0) {
        if(resultCode == RESULT_OK) {
            final boolean isCamera;
            if(returnIntent == null) {
                isCamera = true;
            }
            else
            {
                final String action = returnIntent.getAction();
                if(action == null) {
                    isCamera = false;
                }
                else {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }
            Uri selectedImageUri;
            if(isCamera) {
                selectedImageUri = outputFileUri;
                mainImage.setImageURI(selectedImageUri); //trying full image
            }
            else {
                selectedImageUri = returnIntent == null ? null : returnIntent.getData();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
                    mainImage.setImageBitmap(bitmap); //trying bitmap
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

你的代码是 2000000000000000% 好的我自己测试一下

您的问题是您的 ImageView 由于图像大小而无法显示图像。我用 ImageView 尝试这个代码,如下所示

    <ImageView
    android:id="@+id/mainImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/abc_ab_bottom_solid_dark_holo" />
Run Code Online (Sandbox Code Playgroud)

如果你像这样使用 dp 的高度和宽度

    android:layout_width="100dp"
    android:layout_height="100dp"
Run Code Online (Sandbox Code Playgroud)

您需要压缩 Bitmap 才能将其显示在 ImageView 中。编辑您的代码以进行转换

if(isCamera) {
                selectedImageUri = outputFileUri;
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);//You can use this bitmap if need full image to further use 
                    Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap,  600 ,600, true);//this bitmap2 you can use only for display
                    mainImage.setImageBitmap(bitmap2); //trying full image
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            else {
                selectedImageUri = returnIntent == null ? null : returnIntent.getData();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
                    bitmap = Bitmap.createScaledBitmap(bitmap,  600 ,600, true);
                    mainImage.setImageBitmap(bitmap); //trying bitmap
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
Run Code Online (Sandbox Code Playgroud)