/** * Created by recoil on 26.01.14. */ public class ActArtworks extends Activity { private AQuery aq; private Menu optionsMenu; private boolean refreshing = true; private Activity activity; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_bgr)); // getActionBar().setDisplayHomeAsUpEnabled(true); activity = this; aq = new AQuery(activity); } @Override public boolean onCreateOptionsMenu(Menu menu) { this.optionsMenu = menu; // MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.artwork, menu); // - update(); return super.onCreateOptionsMenu(menu); } public void update() { AQUtility.debug("Update progress"); // "" refreshing = true; // setRefreshActionButtonState(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // finish(); return true; } return super.onOptionsItemSelected(item); } public void setRefreshActionButtonState() { // - if (optionsMenu != null) { final MenuItem refreshItem = optionsMenu .findItem(R.id.menu_refresh); if (refreshItem != null) { if (refreshing) { refreshItem.setActionView(R.layout.actionbar_indeterminate_progress); } else { refreshItem.setActionView(null); } } } } }
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:FreeAmp="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_refresh" android:icon="@drawable/ic_action_refresh" android:title="" android:alphabeticShortcut="r" android:orderInCategory="1" FreeAmp:showAsAction="always" /> </menu>
<style name="theme" parent="@style/Theme.AppCompat">
- refreshItem.setActionView(R.layout.actionbar_indeterminate_progress); + MenuItemCompat.setActionView(refreshItem, R.layout.actionbar_indeterminate_progress);
String url = String.format("http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=0cb75104931acd7f44f571ed12cff105&artist=%s&album=%s&format=json", Uri.encode(track.getArtist()),Uri.encode(currentAlbum)); getHttpData = new GetHttpData(); getHttpData.setUrl(url); getHttpData.request(); String result = new String(getHttpData.getByteArray());
JSONObject jsonObject = new JSONObject(result); jsonObject = jsonObject.getJSONObject("album"); JSONArray image = jsonObject.getJSONArray("image"); for (int i=0;i<image.length();i++) { jsonObject = image.getJSONObject(i); if (jsonObject.getString("size").equals("extralarge")) { albumArtImageLink = Uri.decode(jsonObject.getString("#text")); AQUtility.debug(track.getArtist()+":"+currentAlbum,albumArtImageLink); } }
//download image getHttpData = new GetHttpData(); getHttpData.setUrl(albumArtImageLink); getHttpData.request(); ContentResolver res = activity.getContentResolver(); Bitmap bm = BitmapFactory.decodeByteArray(getHttpData.getByteArray(),0,getHttpData.getByteArray().length);
// Put the newly found artwork in the database. // Note that this shouldn't be done for the "unknown" album, // but if this method is called correctly, that won't happen. // first write it somewhere String file = Environment.getExternalStorageDirectory() + "/albumthumbs/" + String.valueOf(System.currentTimeMillis()); if (FileUtils.ensureFileExists(file)) { try { OutputStream outstream = new FileOutputStream(file); if (bm.getConfig() == null) { bm = bm.copy(Bitmap.Config.RGB_565, false); if (bm == null) { //return getDefaultArtwork(context); } } boolean success = bm.compress(Bitmap.CompressFormat.JPEG, 75, outstream); outstream.close(); if (success) { ContentValues values = new ContentValues(); values.put("album_id", track.getAlbumId()); values.put("_data", file); Uri newuri = res.insert(MediaUtils.sArtworkUri, values); if (newuri == null) { // Failed to insert in to the database. The most likely // cause of this is that the item already existed in the // database, and the most likely cause of that is that // the album was scanned before, but the user deleted the // album art from the sd card. // We can ignore that case here, since the media provider // will regenerate the album art for those entries when // it detects this. success = false; } } if (!success) { File f = new File(file); f.delete(); iterator.remove(); } } catch (FileNotFoundException e) { AQUtility.debug( "error creating file", e); } catch (IOException e) { AQUtility.debug( "error creating file", e); } }
// LruCache: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html + int cacheSize = 20 * 360000; // <7MiB = 300width * 300heigth * 4bytesperpixel * 20images + LruCache bitmapCache = new LruCache(cacheSize) { + protected int sizeOf(int key, Bitmap value) { + return value.getRowBytes() * value.getHeight();// + } + }; + + public void addBitmapToMemoryCache(int key, Bitmap bitmap) { + synchronized (bitmapCache) { + if (getBitmapFromMemCache(key) == null) { + bitmapCache.put(key, bitmap); + } + } + } + + public Bitmap getBitmapFromMemCache(int key) { + return (Bitmap) bitmapCache.get(key); + }
void applyAdapter() { if (tracks == null) return; adapter = new AdpArtworks(activity,tracks); int iDisplayWidth = getResources().getDisplayMetrics().widthPixels ; int numColumns = (iDisplayWidth / 310); gridView.setColumnWidth( (iDisplayWidth / numColumns) ); gridView.setNumColumns(numColumns); gridView.setStretchMode( GridView.NO_STRETCH ) ; gridView.setAdapter(adapter); gridView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { adapter.setScrollState(scrollState); if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { adapter.notifyDataSetChanged(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }); }
+ final Drawable imgBgr = activity.getResources().getDrawable(R.drawable.row_bgr); + final Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + imgBgr.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); + imgBgr.draw(canvas); + this.placeHolder = bitmap;
Source: https://habr.com/ru/post/212125/
All Articles