- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我猜我有内存泄漏。我有一个使用相机的列表 Activity ......现在只处理拍照......但我想一些列表膨胀会导致一些内存泄漏......我想我错过了一些资源释放(猜测图像......)
我找不到它。
在这方面确实需要一些帮助。
以下是类(class):列表 Activity
包org.BJ.Food4All.Activities.NewRecipe;
import org.BJ.Food4All.R;
import org.BJ.Food4All.Recipe;
import org.BJ.Food4All.Recipe.Instruction;
import org.BJ.Food4All.Activities.RecipeBook.RecipeInstructionsListViewAdapter;
import org.BJ.Food4All.Activities.RecipeBook.SharedData;
import org.BJ.Food4All.utils.CameraUtil;
import org.BJ.Food4All.utils.ImageUploadItem;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
public class Instructions extends ListActivity implements OnClickListener
{
private final static String mTAG = "Instructions";
private EditText mInstructionEditText = null;
private RecipeInstructionsListViewAdapter mListViewAdapter = null;
private Recipe mEditRecipe = PrivateResources.GetRecipe();
private CameraUtil mCameraUtil = new CameraUtil( this );
private int mSelectedEntryIndex = -1;
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView( R.layout.new_recipe_instruction_tab );
mInstructionEditText = (EditText)findViewById(R.id.newRecipeInstructionEditTextId);
View addInstructionButton = findViewById( R.id.naddInstructionButtonId );
// Sanity check
if( mInstructionEditText == null ||
addInstructionButton == null )
{
Log.e( mTAG, "NULL pointers");
// secure exit
finish();
}
// Set up click listeners for all the buttons
addInstructionButton.setOnClickListener( this );
mListViewAdapter = new RecipeInstructionsListViewAdapter( this,
R.layout.recipes_instruction_list_single_view_entry,
mEditRecipe.GetInstructions() );
setListAdapter( mListViewAdapter );
registerForContextMenu( getListView() );
}
public void onClick( View v )
{
switch( v.getId() )
{
case R.id.naddInstructionButtonId:
AddInstructionToRecipe( v );
break;
default:
Log.e( mTAG, "Invalid ID:" + v.getId() );
// secure exit
finish();
}
}
private void AddInstructionToRecipe( View v )
{
String instructionText = mInstructionEditText.getText().toString();
if( instructionText == null )
{
return;
}
Instruction newInstruction = new Instruction( mEditRecipe.GetInstructions().size() + 1, // Index
instructionText, // The instruction
null,
true );
if( mEditRecipe.AddInstruction( newInstruction ) != true )
{
// TODO - ERROR
}
else
{
mListViewAdapter.notifyDataSetChanged();
}
}
/*
* (non-Javadoc)
* @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
*/
@Override
public void onCreateContextMenu( ContextMenu menu,
View v,
ContextMenuInfo menuInfo)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.instructions_ctx_menu, menu);
super.onCreateContextMenu( menu,
v,
menuInfo );
}
/*
* (non-Javadoc)
* @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
*/
@Override
public boolean onContextItemSelected(MenuItem item)
{
super.onContextItemSelected(item);
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
mSelectedEntryIndex = menuInfo.position;
switch( item.getItemId() )
{
case R.id.deleteId:
mEditRecipe.RemoveInstruction( mSelectedEntryIndex );
mListViewAdapter.notifyDataSetChanged();
return true;
case R.id.takePictureId:
mCameraUtil.TakePicture();
return true;
}
return false;
}
/*
* (non-Javadoc)
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
*/
@Override
protected void onActivityResult( int requestCode,
int resultCode,
Intent data )
{
String imageLocation = mCameraUtil.onActivityResult( requestCode,
resultCode,
data );
// TODO - switch to parameter passed in the intent!!!! like TakePicture( index );
mEditRecipe.GetInstructions().get( mSelectedEntryIndex ).SetInstructionImageLocation( imageLocation );
mSelectedEntryIndex = -1;
// Update the listviewitem with the picture
mListViewAdapter.notifyDataSetChanged();
}
}
适配器:
package org.BJ.Food4All.Activities.RecipeBook;
import java.util.ArrayList;
import org.BJ.Food4All.R;
import org.BJ.Food4All.Recipe.Instruction;
import org.BJ.Food4All.utils.GlobalDefs;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class RecipeInstructionsListViewAdapter extends ArrayAdapter<Instruction>
{
private Context mContext;
private ArrayList<Instruction> mItems;
private LayoutInflater mInflater;
public RecipeInstructionsListViewAdapter( Context context,
int textViewResourceId,
ArrayList<Instruction> items)
{
super( context,
textViewResourceId,
items );
mContext = context;
mItems = items;
mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView( int position,
View convertView,
ViewGroup parent )
{
ViewHolder holder = new ViewHolder();
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.recipes_instruction_list_single_view_entry, null);
}
if( super.getItem(position) != null )
{
holder.instructionIndex = (TextView) convertView.findViewById( R.id.listUp_RecipeInstructionNumberTextBoxId );
holder.instructionText = (TextView) convertView.findViewById( R.id.listUp_RecipeInstructioTextTextBoxId );
holder.instructionImage = (ImageView)convertView.findViewById( R.id.listUp_RecipeInstructionImageViewId );
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "Eras_Bold.ttf");
holder.instructionIndex.setTypeface(tf);
holder.instructionIndex.setTextSize(30);
holder.instructionIndex.setTextColor( GlobalDefs.GetHeadlineColor() );
holder.instructionIndex.setText( Integer.toString(mItems.get(position).getIndex() ) );
tf = Typeface.createFromAsset(mContext.getAssets(), "Arial.ttf");
holder.instructionText.setTypeface(tf);
holder.instructionText.setTextSize(14);
holder.instructionText.setTextColor( Color.BLACK );
holder.instructionText.setText( mItems.get(position).getText() );
String imageLocation = mItems.get(position).GetInstructionImageLocation();
if( imageLocation != null )
{
holder.instructionImage.setImageURI( Uri.parse( imageLocation ) );
holder.instructionImage.setVisibility( View.VISIBLE );
}
else
{
holder.instructionImage.setVisibility( View.GONE );
}
convertView.setTag(holder);
convertView.setLayoutParams( new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
else
{
}
return convertView;
}
@Override
public boolean isEnabled(int position)
{
return true;
}
static class ViewHolder
{
TextView instructionIndex;
TextView instructionText;
ImageView instructionImage;
}
}
相机实用程序:
package org.BJ.Food4All.utils;
import java.io.File;
import org.BJ.Food4All.DB.DBManager;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
//import android.widget.ImageView;
import android.widget.Toast;
public class CameraUtil
{
private static final String mTAG = "CameraUtil";
private static final int PICK_IMAGE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 2;
private Activity mParentActivity = null;
private String mFileName = null; // Storage filename
// private Uri mImageUri = null; // mImageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
private Bitmap mBitmap = null;
// private ImageView mImageView = null;
private DBManager mDBManager = null;
public CameraUtil( Activity parentActivity )
{
mParentActivity = parentActivity;
mDBManager = new DBManager( parentActivity );
}
/**
* Used by the camera button - for taking a new picture
*/
public void TakePicture()
{
mFileName = mDBManager.GetCurrentImageFilename() + ".jpg";
ContentValues contentValues = new ContentValues();
contentValues.put( MediaStore.Images.Media.TITLE, mFileName );
contentValues.put( MediaStore.Images.Media.DESCRIPTION, "Image capture by camera" ); // TODO- update description for recipe name description
// mImageUri = mParentActivity.getContentResolver().insert(
// MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
// contentValues );
//create new Camera Intent
Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, Uri.fromFile(getImageFile( mFileName )));//mImageUri );
intent.putExtra( MediaStore.EXTRA_VIDEO_QUALITY, 1 );
try
{
mParentActivity.startActivityForResult( intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
}
catch( Exception e )
{
Toast.makeText( mParentActivity.getApplicationContext(),
"Error while starting Camera!",
Toast.LENGTH_LONG ).show();
Log.e( mTAG, "Failed to start camera" );
Log.e( mTAG, e.getMessage(), e );
}
}
/**
*
* @param requestCode
* @param resultCode
* @param data
*/
public String onActivityResult(int requestCode, int resultCode, Intent data)
{
String fileManagerString = null;
String selectedImagePath = null;
switch( requestCode )
{
case PICK_IMAGE:
// Used if we want to choose a picture from the gallery
if( resultCode == Activity.RESULT_OK )
{
Uri selectedImageUri = data.getData();
String filePath = null;
try
{
// OI FILE Manager
fileManagerString = selectedImageUri.getPath();
// MEDIA GALLERY
selectedImagePath = getPath( selectedImageUri );
if( selectedImagePath != null )
{
filePath = selectedImagePath;
}
else if( fileManagerString != null )
{
filePath = fileManagerString;
}
else
{
Toast.makeText( mParentActivity.getApplicationContext(),
"Unknown path",
Toast.LENGTH_LONG ).show();
Log.e( mTAG, "Unknown image path");
}
if( filePath != null )
{
DecodeFile( filePath );
}
else
{
mBitmap = null;
}
}
catch( Exception e )
{
Toast.makeText( mParentActivity.getApplicationContext(),
"Internal error",
Toast.LENGTH_LONG ).show();
Log.e( mTAG, e.getMessage(), e);
}
}
break;
case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
if( resultCode == Activity.RESULT_OK )
{
// Uri selectedImageUri = mImageUri;
String filePath = null;
try
{
// OI FILE Manager
fileManagerString = mFileName;//selectedImageUri.getPath();
// MEDIA GALLERY
// selectedImagePath = mFileName;//getPath( selectedImageUri );
//GlobalData.setUploadedImagePath(selectedImagePath);
// TODO - for uploading the image
// Add image to the recipe images
// ImageUploadItem uploadItem = new ImageUploadItem( selectedImagePath );
// GlobalData.imageUploads.add(uploadItem);
// Get image path on the image
// if( selectedImagePath != null )
// {
// filePath = selectedImagePath;
// }
// else if( fileManagerString != null )
// {
filePath = fileManagerString;
// }
// else
// {
//
// Toast.makeText( mParentActivity.getApplicationContext(),
// "Unknown path",
// Toast.LENGTH_LONG ).show();
//
// Log.e( mTAG, "Unknown image path" );
// }
if( filePath != null )
{
String p = getImageFile( mFileName ).getPath();
DecodeFile( p );//filePath );
}
else
{
mBitmap = null;
}
}
catch( Exception e )
{
Toast.makeText( mParentActivity.getApplicationContext(),
"Internal error",
Toast.LENGTH_LONG ).show();
Log.e( mTAG, e.getMessage(), e);
}
}
break;
default:
return null;
}
// TODO Here is where the image is received from either the camera or the gallery and is in the async task
// TODO to go the next activity
return getImageFile( mFileName ).getPath();//mFileName;//selectedImagePath;
}
/**
*
* @param uri
* @return
*/
private String getPath( Uri uri )
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = mParentActivity.managedQuery( uri,
projection,
null,
null,
null );
if( cursor != null )
{
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor.getColumnIndexOrThrow( MediaStore.Images.Media.DATA );
cursor.moveToFirst();
return cursor.getString( column_index );
}
else
{
return null;
}
}
/**
*
* @param filePath
*/
private void DecodeFile( String filePath )
{
// Decode image size
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile( filePath, bitmapOptions );
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = bitmapOptions.outWidth;
int height_tmp = bitmapOptions.outHeight;
int scale = 1;
while( true )
{
if( width_tmp < REQUIRED_SIZE &&
height_tmp < REQUIRED_SIZE )
{
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options newBitmapOptions = new BitmapFactory.Options();
newBitmapOptions.inSampleSize = scale;
mBitmap = BitmapFactory.decodeFile( filePath, newBitmapOptions );
}
/**
* Gets the picture taken by the camera - to be used in ImageView
*
* @return
*/
public Bitmap GetTakenPictureBitmap()
{
return mBitmap;
}
/**
* Get the image FILE to be used for the picture taken by the camera - from filename String
*
* @param filename - the filename String
* @return The File representing the image file
*/
private File getImageFile( final String filename )
{
//it will return /sdcard/image.tmp
final File path = new File( Environment.getExternalStorageDirectory(),
mParentActivity.getPackageName() );
if( !path.exists() )
{
path.mkdir();
}
return new File( path, filename );
}
}
最佳答案
您很可能保留对 Bitmap 对象的引用,和/或在使用完它们后没有立即recycle()
-ing它们。 99% 的此类错误都是由这引起的。
关于java - OOM 异常 - 位图大小超出 VM 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8054313/
在文档中我们可以找到 The limits are based on a moving window that tracks the number of requests you send per h
我试图了解使用 Windows Azure 托管 Web 服务的正确方法。在阅读了一些可用的文档后,我已经达到以下几行: Windows Azure takes the following actio
我正在使用 unboundid ldap sdk 来执行 ldap 查询。运行 ldap 搜索查询时遇到一个奇怪的问题。当我对包含 50k 个条目的组运行查询时出现异常。我的异常(exception)
我有以下 docker-compose 文件: version: "2.4" services: auto_check: image: python mem_limit: 97M
我有副本集(托管在亚马逊上),其中有: 主要 中学 仲裁者 它们都是 3.2.6 版本,这个副本正在我的分片集群中创建一个分片(如果这很重要,尽管我认为它不重要)。 当我在 primary 上键入 r
我知道在 C++ 中访问缓冲区边界是未定义的行为。 这是来自 cppreference 的示例: int table[4] = {}; bool exists_in_table(int v) {
嗨,我有一个表单的 div。我希望当鼠标离开 div 时禁用单击事件。所以我尝试了这个,但它不起作用,div 仍然可以点击。有什么想法吗?? var flag = false; $("#foo").l
我正在使用我的客户端获取有关存储在我的 Swift 对象存储中的某个文件的一些信息,该文件可以通过 REST Api 访问。在 Swift 中,指向指定对象的 HEAD 方法和 url 返回它的元数据
如何在 Excel 的 CONCATENATE 函数中使用超过 255 个字符?我实际上也在 EXCEL 的 HYPERLINK 函数中使用 CONCATENATE 函数。一个例子如下: =HYPER
在 java 6 web 应用程序中,我尝试从执行的命令中检索大量输出。我在 javaworld article 上“借用/窃取/基于”它。我面临的问题是,由于输出被截断,长度似乎超出了大小限制。我已
我有一个更改事件,当选择框更改时会触发该事件。然而,选择框位于被替换的 div 内,因此会重新生成选择框。由于此错误可能是由于无限循环造成的,因此我猜测创建选择框时也必须触发我的触发事件。我尝试了很多
我正在 visual studio 2013 中用 c# 创建一个网络服务。我已连接到数据库并使用以下代码返回 json。 [WebMethod] [ScriptMethod(ResponseForm
我使用 php 脚本解析远程 xml 文件并将网页上的输出打印到 div 中。由于我需要输出必须与当前播放的轨道同步,所以我使用 Javascript 每 20 秒重新加载一次 div 内容。在测试页
#define MAX_BUFF_SIZE 64 char input[MAX_BUFF_SIZE]; int inSize = read(0, input, MAX_BUFF_SIZE); if
我在申请公司时遇到了问题。 我将总结系统的关键要素: 我公司的系统几年前就在 Windows XP 和 7(家庭版、专业版、基本版)机器上运行。 它是用 .NET 4.0 编写的,基于 WCF。 它使
我有一个渲染循环,用于监听数位板输入并从顶点/索引缓冲区(以及其他内容)中绘制。顶点数据可以增长,当它达到一定水平时,DispatchMsg(&msg) 会遇到这种情况: Unhandled exce
我通过 Postgres JDBC 驱动程序使用 Java 1.7 和 Postgres。将从 Web 服务使用数据库连接。在测试中,我得到了以下错误: FATAL: connection limit
我想知道当超过 Firebase 实时数据库的限制时会发生什么。问题是我知道我可以拥有的最大连接数仅为 100。现在,假设我的 Android 应用程序有 1,000 个活跃用户,并且我实现了实时数据
我正在将一组图像上传到我的 node.js Express 服务器,但收到错误 - “错误:超出 maxFieldsSize”。看起来默认的 maxFieldsSize 是 2MB。我需要能够上传最多
我正在使用 Django 构建一个小型 Web 项目,该项目有一个包含 ImageField 的模型 (Image)。当我尝试使用管理界面上传图片时,我遇到了这个问题(删除了个人身份信息): Runt
我是一名优秀的程序员,十分优秀!