GridViewを使って、内部ストレージからUNIXタイムで指定した期間の写真を引っ張ってきてサムネイルを表示し、写真をタップすると全画面表示になるというアプリを作りたいです。
当初作成した時はエミュレータで、適当な画像をネットからダウンロードして、表示させる試験をしていたのですが、ある程度完成したので実機にAPKを流して、スマホで撮影した写真を使って試験をしてみると、アルバムを開く段階で落ちてしまうようになりました。エラーメッセージを見てみると「java.lang.OutOfMemoryError: Failed to allocate a 518412 byte allocation with 306064 free bytes and 298KB until OOM」と書いているので、恐らくメモリーリークを起こしているんだろうと予想しているのですが、現状のコードの何がメモリーリークの原因なのかがわかりません。
教えてください。
よろしくお願いいたします。
LogAlbumActivity.java
java
1public class LogAlbumActivity extends AppCompatActivity 2 implements AdapterView.OnItemClickListener{ 3 4 private Context context; 5 private Log log; 6 7 private ArrayList<Bitmap> lstBitmap = new ArrayList<>(); 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 context = getApplicationContext(); 13 setContentView(R.layout.activity_log_album); 14 15 log = (Log)getIntent().getExtras().get(EXTRA_LOG); 16 17 initView(); 18 } 19 20 @Override 21 protected void onResume() { 22 super.onResume(); 23 FlurryLogger.logEvent(getClass().getSimpleName() + " onResume"); 24 } 25 26 @Override 27 public boolean onOptionsItemSelected(MenuItem item) { 28 switch (item.getItemId()){ 29 case android.R.id.home: 30 finish(); 31 break; 32 } 33 return super.onOptionsItemSelected(item); 34 } 35 36 @Override 37 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {} 38 39 //========================================================================================== 40 // private method 41 //========================================================================================== 42 43 private void initView(){ 44 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 45 setSupportActionBar(toolbar); 46 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 47 48 if(log.getType() == Log.Type.gps.ordinal() || log.getType() == Log.Type.manual.ordinal()) { 49 if(!Locale.JAPAN.equals(Locale.getDefault())) { 50 getSupportActionBar().setTitle(log.getTestEnglish()); 51 }else { 52 getSupportActionBar().setTitle(log.getTestName()); 53 } 54 }else{ 55 getSupportActionBar().setTitle(R.string.title_activity_log); 56 } 57 58 if(log.getType() != Log.Type.file.ordinal()) { 59 Calendar calendar = Calendar.getInstance(); 60 calendar.setTimeInMillis(log.getDateTime()); 61 if(log.getType() == Log.Type.gpx.ordinal()){ 62 calendar.setTimeInMillis(log.getStartTime()); 63 getSupportActionBar().setSubtitle(DateFormat.format( 64 this.getResources().getString(R.string.date_time_format), calendar) + " ~"); 65 }else if(log.getType() == Log.Type.gps.ordinal()){ 66 getSupportActionBar().setSubtitle(DateFormat.format( 67 this.getResources().getString(R.string.date_time_format), calendar)); 68 }else{ 69 getSupportActionBar().setSubtitle(DateFormat.format( 70 this.getResources().getString(R.string.date_format), calendar)); 71 } 72 } 73 74 GridInit(); 75 } 76 77 private void GridInit(){ 78 79 GridView gridView = (GridView)findViewById(R.id.gridView); 80 81 //表示する画像のURIを取得 82 Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; //本体、SDカードの場合はINTERNAL_CONTENT_URI 83 84 // 何時間か 85 long term; 86 // 終了時間 87 String EndTime; 88 // 開始時間 89 String StartTime; 90 91 if(log.getType() == Log.Type.gps.ordinal() || log.getType() == Log.Type.manual.ordinal()) { 92 term = 86400; 93 StartTime = Long.toString(log.getDateTime() - 14400000); 94 EndTime = Long.toString((term + (log.getDateTime()/1000)) + 14400000); 95 }else{ 96 // 終了時間をGPXファイルから取る場合 97 term = log.getTime() * 60; 98 EndTime = Long.toString((term + (log.getStartTime()/1000)) + 14400000); 99 StartTime = Long.toString(log.getStartTime() - 14400000); 100 } 101 102 // 共通に必要な処理 103 StartTime = StartTime.substring(0,StartTime.length()-3); 104 105 // 正しいコード 106 String[]mSelectionArgs = { StartTime, EndTime }; 107 108 // WHERE句にBETWEENを入れてXからYまでの検索条件を入れる 109 Cursor cursor = getContentResolver().query(uri, null, "date_added BETWEEN ? AND ?", mSelectionArgs, null); 110 ContentResolver cr = getContentResolver(); 111 112 cursor.moveToFirst(); 113 114 final List<Long> longList = new ArrayList<>(); 115 116 for (int i = 0; i < cursor.getCount(); i++){ 117 long id = cursor.getLong(cursor.getColumnIndexOrThrow("_id")); 118 longList.add(id); 119 Bitmap bmp = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null); 120 lstBitmap.add(bmp); 121 cursor.moveToNext(); 122 } 123 //アダプター作成 124 LogAlbumAdapter adapter = new LogAlbumAdapter(getApplicationContext(), lstBitmap); 125 //グリッドにアダプタを設定 126 gridView.setAdapter(adapter); 127 128 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 129 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 130 131 Intent intent = new Intent(context, PhotoActivity.class); 132 intent.putExtra(PhotoActivity.EXTRA_PHOTO, longList.get(position)); 133 startActivity(intent); 134 } 135 }); 136 }
activity_log_album.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2 3<android.support.design.widget.CoordinatorLayout 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 9 <android.support.design.widget.AppBarLayout 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:theme="@style/AppTheme.AppBarOverlay"> 13 14 <android.support.v7.widget.Toolbar 15 android:id="@+id/toolbar" 16 android:layout_width="match_parent" 17 android:layout_height="?attr/actionBarSize" 18 android:background="?attr/colorPrimary" 19 app:popupTheme="@style/AppTheme.PopupOverlay"/> 20 21 </android.support.design.widget.AppBarLayout> 22 <LinearLayout 23 android:layout_width="fill_parent" 24 android:layout_height="match_parent" 25 android:layout_marginTop="?attr/actionBarSize" 26 android:orientation="vertical" 27 android:paddingTop="5dp" 28 android:paddingLeft="10dp"> 29 <GridView 30 android:id="@+id/gridView" 31 android:layout_width="match_parent" 32 android:layout_height="match_parent" 33 android:numColumns="auto_fit" 34 android:verticalSpacing="30dp" 35 android:horizontalSpacing="30dp" 36 android:columnWidth="150dp" 37 android:stretchMode="columnWidth" 38 android:gravity="center"> 39 </GridView> 40 </LinearLayout> 41</android.support.design.widget.CoordinatorLayout>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。