【问题标题】:View too large to fit into drawing cache视图太大而无法放入绘图缓存
【发布时间】:2016-02-08 17:38:35
【问题描述】:

相机功能真的让我抓狂!我有活动 A 和活动 B,其中选定的图像将从活动 B 返回到 A。我使用下面的代码,但它并不适用于所有情况。一些选定的图像可以在 Activity A 中显示,但有些则不能。

活动 B

 b = (ImageView) findViewById(R.id.imageView3);
  @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                  // Take photo

            } else if (requestCode == 2) {

                Uri selectedImage = data.getData();
                // h=1;
                //imgui = selectedImage;
                String[] filePath = {MediaStore.Images.Media.DATA};
                Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                Log.w("path of image ******", picturePath + "");
                b.setImageBitmap(thumbnail);
            }


        }
        else
        {
            finish();
        }


    }

}

   ok.setOnClickListener(new View.OnClickListener() // Return to Activity A
        {
            public void onClick(View arg0)
            {
                Intent returnIntent=new Intent();
                b.setDrawingCacheEnabled(true);
                b.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                b.layout(0, 0, b.getMeasuredWidth(), b.getMeasuredHeight());
                b.buildDrawingCache(true);
                returnIntent.putExtra("text", text);
                if (b.getDrawingCache() != null) {
                    Bitmap bitmap = Bitmap.createBitmap(b.getDrawingCache());
                    if (bitmap == null) {
                        Log.e("TAG", "getDrawingCache() == null");
                    }
                    Global.img = bitmap;
                }
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

活动 A

  viewImage = (ImageView) findViewById(R.id.imageView2);

 public void onActivityResult(int requestCode,int resultCode, Intent data)
    {
        if(requestCode==PROJECT_REQUEST_CODE) {
            if(data!=null&&data.hasExtra("text")) {
                c = data.getStringExtra("text");
                txt1.setText(c);
                viewImage.setImageBitmap(Global.img); // Not all image can be displayed here !!!
            }


        }
        else if (requestCode==CAMERA_REQUEST_CODE)
        {

        }
    }

}

Global.java

public class Global {

    static Bitmap img;
}

11-08 01:39:19.820 8675-8675/com.example.project.project W/路径 图片******:/storage/extSdCard/DCIM/100MSDCF/DSC00050.jpg 11-08 01:39:27.730 8675-8675/com.example.project.project W/View: 查看 大到适合绘图缓存,需要 12582912 字节,只有 3686400 可用 11-08 01:39:27.730 8675-8675/com.example.project.project W/View:视图太大,无法放入绘图缓存,需要 12582912 字节,只有 3686400 可用

activity_b

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="574dp"
        android:layout_height="523dp"
        android:id="@+id/imageView3"
        android:layout_x="6dp"
        android:layout_y="0dp" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/editText38"
        android:layout_x="4dp"
        android:hint="Add caption"
        android:layout_y="491dp" />
    <Button
        android:layout_width="174dp"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:id="@+id/button15"
        android:layout_x="8dp"
        android:layout_y="571dp" />
    <Button
        android:layout_width="174dp"
        android:layout_height="wrap_content"
        android:text="OK"
        android:id="@+id/button16"
        android:layout_x="195dp"
        android:layout_y="571dp" />
</AbsoluteLayout>

activity_a

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="47dp"
        android:id="@+id/textView1"
        android:background="#000000"
        android:padding="5dp"
        android:text="Add Project"
        android:textColor="#FFFFFF"/>

    <TextView
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Project"
        android:id="@+id/textView48"
        android:layout_x="47dp"
        android:layout_y="82dp" />

    <EditText
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:hint="Amount"
        android:id="@+id/editText36"
        android:layout_x="47dp"
        android:layout_y="133dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/button17"
        android:layout_x="47dp"
        android:layout_y="560dp" />

    <ImageButton
        android:layout_width="66dp"
        android:layout_height="54dp"
        android:id="@+id/imageButton"
        android:src="@mipmap/camerabutton"
        android:layout_x="277dp"
        android:layout_y="149dp"
        xmlns:android="http://schemas.android.com/apk/res/android" />

    <ImageView
        android:layout_width="293dp"
        android:layout_height="283dp"
        android:id="@+id/imageView2"
        android:layout_x="41dp"
        android:layout_y="223dp" />

    <TextView
        android:layout_width="304dp"
        android:layout_height="51dp"
        android:id="@+id/textView57"
        android:layout_x="43dp"
        android:layout_y="490dp" />

</AbsoluteLayout>

选中的图片首先会放在imageView3(Activity B)中。点击button16时,图片会显示在imageView2中。(活动A)

【问题讨论】:

  • 你能在你设置 Image Bitmap 的地方显示你的 layout.xml 文件吗
  • @PavitraKansara 嗨,感谢您的回复。我已经编辑了我的帖子。

标签: java android image caching android-camera


【解决方案1】:

尝试在ImageView 中设置缩放位图。这样,您将获得按比例缩小的 Bitamp。 因此,在您将Bitmap 设置为ImageView 的代码中,调用getScaledBitmap 并根据您的需要提供宽度和高度的输入参数。

private Bitmap getScaledBitmap(String picturePath, int width, int height) {
    BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
    sizeOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(picturePath, sizeOptions);

    int inSampleSize = calculateInSampleSize(sizeOptions, width, height);

    sizeOptions.inJustDecodeBounds = false;
    sizeOptions.inSampleSize = inSampleSize;

    return BitmapFactory.decodeFile(picturePath, sizeOptions);
}


private int calculateInSampleSize(BitmapFactory.Options options,
                                      int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
     }
    return inSampleSize;
}

根据您的View,使用所需的height 和width 调用getScaledBitmap()

image.setImageBitmap(getScaledBitmap(picturePath, WIDTH, HEIGHT));

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多