视图太大,无法放入绘图缓存
View too large to fit into drawing cache
拍照功能真是让我抓狂!我有 activity A 和 Activity B,其中所选图像将从 Activity B 到 A return。我使用下面的代码,但它不适用于所有情况。一些选定的图像可以在 Activity A 中显示,但有些则不能。
Activity 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();
}
});
Activity一个
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/path of
image ******﹕ /storage/extSdCard/DCIM/100MSDCF/DSC00050.jpg 11-08
01:39:27.730 8675-8675/com.example.project.project W/View﹕ View too
large to fit into drawing cache, needs 12582912 bytes, only 3686400
available 11-08 01:39:27.730 8675-8675/com.example.project.project
W/View﹕ View too large to fit into drawing cache, needs 12582912
bytes, only 3686400 available
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
.(Activity A)
尝试获取缩放位图以在 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));
希望对您有所帮助。
拍照功能真是让我抓狂!我有 activity A 和 Activity B,其中所选图像将从 Activity B 到 A return。我使用下面的代码,但它不适用于所有情况。一些选定的图像可以在 Activity A 中显示,但有些则不能。
Activity 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();
}
});
Activity一个
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/path of image ******﹕ /storage/extSdCard/DCIM/100MSDCF/DSC00050.jpg 11-08 01:39:27.730 8675-8675/com.example.project.project W/View﹕ View too large to fit into drawing cache, needs 12582912 bytes, only 3686400 available 11-08 01:39:27.730 8675-8675/com.example.project.project W/View﹕ View too large to fit into drawing cache, needs 12582912 bytes, only 3686400 available
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
.(Activity A)
尝试获取缩放位图以在 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));
希望对您有所帮助。