Getting the cover image of song from local media - java

ContentResolver musicResolver = getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri,null,null,null,null);
if(musicCursor!=null && musicCursor.moveToFirst()){
int idColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
int titleColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int artistColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int imageColumn = musicCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
//Add songs to the array list
do{
String theArt = musicCursor.getString(imageColumn);
Bitmap image = BitmapFactory.decodeFile(theArt);
long songId = musicCursor.getLong(idColumn);
String songTitle = musicCursor.getString(titleColumn);
String songArtist = musicCursor.getString(artistColumn);
songsArrayList.add(new Songs(songId,songTitle,songArtist,image));
}while (musicCursor.moveToNext())
I want to get the cover image of the song which i am getting from the local storage, I have no idea how to do it so i get
int imageColumn =
musicCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
this code on internet to get album art this is showing error when i get string from it . That accessing column -1 , I think its not getting the right column to get the image , please let me know how to get the image from media in the way of above code
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.downloadbro.mumusic, PID: 10877
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.downloadbro.mumusic/com.downloadbro.mumusic.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2755)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2816)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1555)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6383)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at android.database.CursorWrapper.getString(CursorWrapper.java:137)
at com.downloadbro.mumusic.MainActivity.getSongList(MainActivity.java:80)
at com.downloadbro.mumusic.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6861)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
And this may give me album art but i want media art no function as such showing to me

Related

How to fix Integer.intValue() for RTL languages in Android

In my application I write some codes and this codes I used string.
I want use UTF-8 languages (Farsi), But after run application show me error and force close application.
But when use English language not show me any error and run successfully!!
My codes :
this.q.put("dry land swim", Integer.valueOf(R.array.dry_land_swim));
TypedArray obtainTypedArray = getResources().obtainTypedArray(((Integer) this.q.get(stringArray[i])).intValue());
int length = obtainTypedArray.length();
int[] iArr2 = new int[length];
WorkoutData workoutData = new WorkoutData();
for (int i2 = 0; i2 < length; i2++) {
iArr2[i2] = obtainTypedArray.getResourceId(i2, -1);
}
workoutData.setExcName(stringArray[i]);
Log.e("ShowError", "" + (this.r.get(stringArray[i])).intValue());
workoutData.setExcDescResId(((Integer) this.r.get(stringArray[i])).intValue());
When change this line text (from En to Fa) :
this.q.put("تست", Integer.valueOf(R.array.dry_land_swim));
Show me error for this lines :
TypedArray obtainTypedArray = getResources().obtainTypedArray(((Integer) this.q.get(stringArray[i])).intValue());
workoutData.setExcDescResId(((Integer) this.r.get(stringArray[i])).intValue());
Error message :
java.lang.RuntimeException: Unable to start receiver com.crazytrends.healthmanager.WalkandStep.receivers.HardwareStepCountReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.crazytrends.healthmanager/.WalkandStep.services.HardwareStepCounterService }: app is in background uid UidRecord{528e8bd u0a841 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
at android.app.ActivityThread.handleReceiver(ActivityThread.java:4452)
at android.app.ActivityThread.access$1600(ActivityThread.java:301)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2159)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.crazytrends.healthmanager/.WalkandStep.services.HardwareStepCounterService }: app is in background uid UidRecord{528e8bd u0a841 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1795)
at android.app.ContextImpl.startService(ContextImpl.java:1740)
at android.content.ContextWrapper.startService(ContextWrapper.java:738)
at android.content.ContextWrapper.startService(ContextWrapper.java:738)
at com.crazytrends.healthmanager.WalkandStep.receivers.HardwareStepCountReceiver.onReceive(HardwareStepCountReceiver.java:22)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:4443)
How can I Fix it?

Error while converting String to Integer in android

I am trying to convert string value into int but it give me error:
android.content.res.resources$notfoundexception string resource id #0x16 <
I tried parsing like:
String date = "29-30-2098";
String[] d = date.split("-");
int di = Integer.parseInt(d[1]);
Toast.makeText(MainActivity.this, di, Toast.LENGTH_SHORT).show();
if i give String data type to "di" then it print the correct value. But when i try to parse it to Int it give the error
And i also tried by using ValueOf(); but in all methods it give me error.
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myanxietyjournal, PID: 8830
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myanxietyjournal/com.myanxietyjournal.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1e
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1e
at android.content.res.HwResources.getText(HwResources.java:442)
at android.widget.Toast.makeText(Toast.java:307)
at com.myanxietyjournal.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:6915)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:156) 
at android.app.ActivityThread.main(ActivityThread.java:6523) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 
There are two versions of makeText:
makeText(Context context, CharSequence text, int duration)
makeText(Context context, int resId, int duration)
The first one takes a String as the second parameter, and shows the message in the string to the user.
The second one takes a resource id as the second parameter, tries to lookup the resource, and fails because you don't have a resource with that id.
To show the int value to the user, you need to convert it to string first, e.g. String.valueOf(di).

when click my button app is stop and error in android java

I'm new in android development and am getting this errors when click my "getir" button. It error after work. How to solve this issue
my code code:
public void getir(View view)
{
cursor = myData.rawQuery("SELECT * FROM kolonVeri", null);
int gunIx = cursor.getColumnIndex("gun");
int vtarihIx = cursor.getColumnIndex("vTarih");
cursor.moveToFirst();
while (cursor != null)
{
System.out.println("VARDİYA: " + cursor.getString(gunIx));
System.out.println("TARİH: " + cursor.getString(vtarihIx));
cursor.moveToNext();
}
}
error log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hakannacar.vardiyam, PID: 19541
java.lang.IllegalStateException: Could not execute method for android:onClick
om.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4
A returned Cursor (e.g. as from rawQuery) will not be null, the check for null is useless and as shown in this case dangerous.
Rather you should be checking the Cursor move??? methods to see if they return true.
The actual issue is that you end up trying to retrieve data for the 5th row (Index 4) when there are only 4 rows (size), as the check for != null will always be true, so the loop continues beyond the 4th row.
As such your code could be :-
public void getir(View view)
{
cursor = myData.rawQuery("SELECT * FROM kolonVeri", null);
int gunIx = cursor.getColumnIndex("gun");
int vtarihIx = cursor.getColumnIndex("vTarih");
while (cursor.moveToNext()) {
System.out.println("VARDİYA: " + cursor.getString(gunIx));
System.out.println("TARİH: " + cursor.getString(vtarihIx));
}
}
You have to check if the cursor has more items, replace your while loop with this
if (cursor.moveToFirst()) {
do {
System.out.println("VARDİYA: " + cursor.getString(gunIx));
System.out.println("TARİH: " + cursor.getString(vtarihIx));
} while (cursor.moveToNext());
}

How to use a Cursor?

I have an app that receives the id of an animal and pass that id to a method from the database that will be read several times in a do-while. I want to know why this error happens?
This is my code:
bd = new BaseDados(getApplicationContext());
Cursor cc = bd.getIdAnimal(chipnumber);
if (cc.moveToFirst()) {
idanimal = cc.getInt(cc.getColumnIndex("idanimal"));
}
Cursor ccc = bd.getGruupsnosAnimals(idanimal);
do{
if(ccc.moveToFirst()){
String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
}
}while (ccc.moveToNext());
This is my error:
Process: com.example.nobre.myapplication, PID: 4529
android.database.sqlite.SQLiteException: not an error (code 0)
at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:845)
at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
at com.example.nobre.myapplication.Activities.VerAnimaisActivity.showDialog(VerAnimaisActivity.java:193)
at com.example.nobre.myapplication.Activities.VerAnimaisActivity$2.onClick(VerAnimaisActivity.java:153)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Exception: not an error? That's a new one.
Your bottom loop is a bit weird and may be a bug- you're moving to next but may not even have moved to first if that failed. And if it succeeded you'll infinitely loop. Try this instead:
if(ccc.moveToFirst()){
do{
String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
}while (ccc.moveToNext());
}
do{
if(ccc.moveToFirst()){
String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
}
}while (ccc.moveToNext());
this is an infinite loop, while you have next item in your cursor, you move to first one, and next iteration you would again move to first one.
Also do not forget to call Cursor.close() after usage.
Try this:
// Move the cursor to the first row if cursor is not empty
if(ccc.moveToFirst()) {
do{
String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
}while (ccc.moveToNext()); // Move cursor to next row until it pass last entry
}
Instead of:
do{
if(ccc.moveToFirst()) {
String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
}
}while (ccc.moveToNext());

Out of Memory on Samsung Galaxy devices

I've really tried my best, but I just can't find a fix for this Problem.
I wrote a Soundboard for Android devices, where every sound as well as its button images is loaded dynamically.
Here is the crucial paragraph in my code:
private void loadButtonColors(){
int rescount = AppConfig.BUTTONBACKGROUNDSRES(getApplicationContext()).size();
SharedPreferences prefs = getSharedPreferences("buttoncolors", 0);
String colors = null;
colors = prefs.getString("colors", null);
List<String> tempList = new ArrayList<String>();
if(colors != null){
tempList.addAll(Arrays.asList(TextUtils.split(colors, "~")));
}
if(colors == null || tempList.size() != MainActivity.SOUNDBUTTONS.size()){
for(#SuppressWarnings("unused") SoundButton s : MainActivity.SOUNDBUTTONS){
int randomint = new Random().nextInt(rescount);
Integer randominteger = Integer.valueOf(randomint);
String randomstring = String.valueOf(randominteger);
tempList.add(randomstring);
}
prefs.edit().putString("colors", TextUtils.join("~", tempList)).commit();
}
MainActivity.BUTTONCOLORS = (ArrayList<String>) tempList;
}
Later the button's images will be displayed with the images out of the following Arrays where the error seems to arise.
public static ArrayList<Drawable> BUTTONBACKGROUNDSRES(Context context){
ArrayList<Drawable> tempList = new ArrayList<Drawable>();
tempList.add(context.getResources().getDrawable(R.color.button_lblue));
tempList.add(context.getResources().getDrawable(R.color.button_lime));
tempList.add(context.getResources().getDrawable(R.color.button_orange));
tempList.add(context.getResources().getDrawable(R.color.button_pink));
tempList.add(context.getResources().getDrawable(R.color.button_red));
tempList.add(context.getResources().getDrawable(R.color.button_yellow));
return tempList;
};
The resulting error looks like this:
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2150)
at android.content.res.Resources.getDrawable(Resources.java:715)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2132)
at android.content.res.Resources.getDrawable(Resources.java:715)
at com.centivo.inscope21soundboard.AppConfig.BUTTONBACKGROUNDSRES(AppConfig.java:45)
at com.centivo.inscope21soundboard.LauncherActivity.loadButtonColors(LauncherActivity.java:128)
at com.centivo.inscope21soundboard.LauncherActivity.access$1(LauncherActivity.java:127)
at com.centivo.inscope21soundboard.LauncherActivity$1.run(LauncherActivity.java:43)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
If you wanna check out the App visit the Playstore
EDIT: The size of every button image is about 70kb.
If you are running out of memory, store the tempList in BUTTONBACKGROUNDSRES in a member or static variable. It looks like every time you call BUTTONBACKGROUNDSRES, you are going to load a new set of Drawables. If these are always the same, there is no sense in loading them frequently from scratch and then leaving the tempList to be garbage collected. Just create the list once, then refer it.

Categories

Resources