Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have developed a List view , Imageview application for videoplayer. I have implemented this code to get the videos from SD Card. But i am finding one issue when navigate from Videoview to listview and back to videoview at
Java
id = (videoCursor.getString(videoColumnIndex));


COde is as below

Java
package shashank_balaganchi.sbsvideoplayer;

/**
 * Created by shashank on 04-10-2015.
 */

import android.app.*;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.*;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.*;
import android.provider.*;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.AdapterView.*;
public class Videolist extends Activity {

    private Cursor videoCursor;
    private int videoColumnIndex;
    ListView videolist;
    int count;
    String thumbPath;
    SwipeRefreshLayout swipeLayout;

    String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,MediaStore.Video.Thumbnails.VIDEO_ID };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filelist);

        initialization();
    }

    @SuppressWarnings("deprecation")
    public void initialization()
    {
        System.gc();
        String[] videoProjection = { MediaStore.Video.Media._ID,MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE };
        videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,videoProjection, null, null, null);
        count = videoCursor.getCount();

        videolist = (ListView) findViewById(R.id.PhoneVideoList);
        videolist.setAdapter(new VideoAdapter(this.getApplicationContext()));
        videolist.setOnItemClickListener(videogridlistener);
    }
     private OnItemClickListener videogridlistener = new OnItemClickListener()
     {

        public void onItemClick(AdapterView parent, View v, int position,long id)
        {
            System.gc();
            videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            videoCursor.moveToPosition(position);
            String filename = videoCursor.getString(videoColumnIndex);
            Log.i("FileName: ", filename);
            Intent intent = new Intent(Videolist.this,ViewVideo.class);
                intent.putExtra("videofilename", filename);
                 startActivity(intent);
        }
     };

    public class VideoAdapter extends BaseAdapter
    {
        private Context vContext;
        int layoutResourceId;

        public VideoAdapter(Context c)
        {
            vContext = c;
        }

        public int getCount()
        {
            return count;
        }

        public Object getItem(int position)
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            System.gc();
            ViewHolder holder;
            String id = null;
            convertView = null;
            if (convertView == null)
                convertView = LayoutInflater.from(vContext).inflate(R.layout.listview, parent, false);
            holder = new ViewHolder();

            holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
            holder.txtSize = (TextView) convertView.findViewById(R.id.txtSize);
            holder.thumbImage = (ImageView) convertView.findViewById(R.id.imgIcon);

            videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
            videoCursor.moveToPosition(position);
             id = (videoCursor.getString(videoColumnIndex));

            videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
            videoCursor.moveToPosition(position);
            // id += " Size(KB):" + videoCursor.getString(videoColumnIndex);
            holder.txtTitle.setText(id);
            holder.txtSize.setText(" Size(KB):" + videoCursor.getString(videoColumnIndex));

            String[] proj = {MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA};
            @SuppressWarnings("deprecation")
            Cursor cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Video.Media.DISPLAY_NAME + "=?", new String[]{id}, null);
            cursor.moveToFirst();
            long ids = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID));

            ContentResolver crThumb = getContentResolver();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, ids, MediaStore.Video.Thumbnails.MICRO_KIND, options);
            holder.thumbImage.setImageBitmap(curThumb);
            curThumb = null;
         return convertView;
    }
}
    static class ViewHolder {

        TextView txtTitle;
        TextView txtSize;
        ImageView thumbImage;
    }

}



Java
Process: shashank_balaganchi.sbsvideoplayer, PID: 23221
    android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5
            at android.database.AbstractCursor.checkPosition(AbstractCursor.java:432)
            at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
            at android.database.CursorWrapper.getString(CursorWrapper.java:114)
            at shashank_balaganchi.sbsvideoplayer.Videolist$VideoAdapter.getView(Videolist.java:114)
            at android.widget.AbsListView.obtainView(AbsListView.java:2257)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
            at android.widget.ListView.onMeasure(ListView.java:1175)
            at android.view.View.measure(View.java:16508)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16508)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16508)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
            at android.view.View.measure(View.java:16508)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
            at android.view.View.measure(View.java:16508)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1955)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1152)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1334)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1039)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5648)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            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:5052)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)



PLease help me to resolve this .
Posted

Android is still based on Java language, so the exceptions generally are Java-themed problems in your applications. The "Index out of bounds" means that you are trying to reference an element that is not available inside the current collection.

Have a look at the exception itself,

Index -1 requested, with a size of 5


It should start from 0 and end at 4 (5th element). You are trying to call the element at -1; which doesn't exist. Thus it throws this error. You should consider checking how you are working with the Cursor objects. Debugging would help you. :-)
 
Share this answer
 
Comments
ridoy 25-Oct-15 14:54pm    
5ed!
Afzaal Ahmad Zeeshan 25-Oct-15 15:05pm    
Thank you.
Quote:
Index out of range

The error message tells you all, It means that you have an array and you try to access a place in the array that do not exist.
The error dump tells you exactly what you tried and where:
Quote:
Index -1 requested, with a size of 5

The only advice is to run your program with the debugger until you get to the place where it crash and then inspect the variables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900