私のデータベースにイメージを保存します。また、イメージとタイトルがすでにデータベースに入っているかどうか確認したいと思います。そうであれば、データベースに追加されません。これは私のクラスです。sqlite dbにイメージを格納する際のjava.lang.OutOfMemoryError
観光
public class Attractions extends ListActivity {
DataBaseHandler db = new DataBaseHandler(this);
ArrayList<Contact> imageArry = new ArrayList<Contact>();
List<Contact> contacts;
ContactImageAdapter adapter;
int ctr, loaded;
int [] landmarkImages={R.drawable.oblation,R.drawable.eastwood,R.drawable.ecopark,R.drawable.circle};
String []landmarkDetails = { "Oblation", "Eastwood", "Ecopark", "QC Circle"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attractions);
ctr = db.checkContact(landmarkDetails[loaded]);
// get image from drawable
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
for(loaded=0; loaded <landmarkDetails.length;loaded++){
Bitmap image = BitmapFactory.decodeResource(getResources(),
landmarkImages[loaded]);
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.d("Going to load images", "Image "+ loaded);
Log.d("Goind to load objects", "loading");
if(ctr == 0){
Log.d("Nothing Loaded", "Loading Now");
db.addContact(new Contact(landmarkDetails[loaded], imageInByte));}
Log.d(landmarkDetails[loaded], "Loaded!");
image.recycle();
}
loadFromDb();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.attractions, menu);
return true;
}
public void loadFromDb(){
// Reading all contacts from database
contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage();
// Writing Contacts to log
Log.d("Result: ", log);
//add contacts data in arrayList
imageArry.add(cn);
}
adapter = new ContactImageAdapter(this, R.layout.screen_list,
imageArry);
ListView dataList = (ListView) findViewById(android.R.id.list);
dataList.setAdapter(adapter);
}
public void onPause(){
super.onPause();
}
public void onResume(){
super.onResume();
}
}
それは、エミュレータ上で正常に動作しますが、私は私のS4でのテストを試してみましたが、その後、このクラスに行くの3回の試行の後に、それは停止を余儀なくされました。私はUSBデバッグでそれを試して、logcatはjava.lang.outofmemoryerror
を示した。 logcatは、私のcontactimageアダプターのエラーを指摘しました。
ContactImageAdapter
public class ContactImageAdapter extends ArrayAdapter<Contact>{
Context context;
int layoutResourceId;
// BcardImage data[] = null;
ArrayList<Contact> data=new ArrayList<Contact>();
public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
Contact picture = data.get(position);
holder.txtTitle.setText(picture._name);
//convert byte to bitmap take from contact class
byte[] outImage=picture._image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
そして、私は画像を管理し、それらを保存するにはほとんど(ほとんどない)の知識を持って、このラインにBitmap theImage = BitmapFactory.decodeStream(imageStream);
を指摘しました。私はandroid:largeHeap
も有効にしますが、複数の試行で強制的にクローズします。誰かがこの問題を解決するのを手伝ってくれることを願っています。少なくとも、sqlite dbにテキストと画像を保存する方法を教えてください。どうもありがとう!
一般に、アプリケーションディレクトリ(外部)にファイルを保存し、必要に応じてデータベース内のファイルパスを維持することをおすすめします。複雑な操作 –
http://developer.androidの指示に従ってください。com/training/displaying-bitmaps/load-bitmap.html –
画像の大きさはどれくらいですか?おそらく、保存する前にサイズを変更して圧縮する必要があります。アマチュアの写真家としては、ほとんどの場合、同じ画像で何度もやってはいけないと仮定して、ほとんどの場合70%という品質に目立った影響を与えずに、少なくとも80%までJPEGを圧縮できることがわかります。あなたは現在、100%の品質比率を持っています。サイズを変更して圧縮すると、必要なサイズとそれに必要なメモリ容量に大きな違いが生じます。 –