質問

I want to get the Thumbnails of image and video file by using MediaStore.Images.Thumbnails.getThumbnail.

I have multiple file in a folder. I use the following code to get the Thumbnails of image and video file.

It is working , and I can get the Thumbnails of image and video file by using following code.

String[] projection_image = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, };
            String[] projection_video = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, };

            File file = new File(viewTag.mFileNode.mName) ;
            String tempfilePath = file.getPath();

            String whereClause_image = MediaStore.Images.Media.DATA + " = '" + tempfilePath + "'";
            String whereClause_video = MediaStore.Video.Media.DATA + " = '" + tempfilePath + "'";

            ContentResolver cr = getActivity().getContentResolver();
            Cursor image_cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection_image, whereClause_image, null, null);
            Cursor video_cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection_video, whereClause_video, null, null);

            if(image_cursor.moveToFirst()){
                long thumID = image_cursor.getLong(image_cursor.getColumnIndex("_ID"));
                Bitmap bitmap_image = MediaStore.Images.Thumbnails.getThumbnail(getActivity().getContentResolver(), thumID, Images.Thumbnails.MICRO_KIND, null);
                viewTag.mThumbnail.setImageBitmap(bitmap_image) ;

            }

            if(video_cursor.moveToFirst()){
                long thumID = video_cursor.getLong(video_cursor.getColumnIndex("_ID"));
                Bitmap bitmap_video = MediaStore.Video.Thumbnails.getThumbnail(getActivity().getContentResolver(), thumID, Video.Thumbnails.MICRO_KIND, null);
                viewTag.mThumbnail.setImageBitmap(bitmap_video) ;

            }

But when I reload this page several time , it show the java.lang.NullPointerException

The error log is like the following:

D/AndroidRuntime( 3014): Shutting down VM
W/dalvikvm( 3014): threadid=1: thread exiting with uncaught exception (group=0x410e2450)
E/AndroidRuntime( 3014): FATAL EXCEPTION: main
E/AndroidRuntime( 3014): java.lang.NullPointerException
E/AndroidRuntime( 3014):    at tw.com.a_i_t.IPCamViewer.FileBrowser.LocalFileBrowserFragment$LocalFileListAdapter.getView(LocalFileBrowserFragment.java:950)
E/AndroidRuntime( 3014):    at android.widget.AbsListView.obtainView(AbsListView.java:2255)
E/AndroidRuntime( 3014):    at android.widget.GridView.onMeasure(GridView.java:1045)
E/AndroidRuntime( 3014):    at android.view.View.measure(View.java:15367)
E/AndroidRuntime( 3014):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
E/AndroidRuntime( 3014):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1420)
E/AndroidRuntime( 3014):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
E/AndroidRuntime( 3014):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
E/AndroidRuntime( 3014):    at android.view.View.measure(View.java:15367)
E/AndroidRuntime( 3014):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:622)
E/AndroidRuntime( 3014):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:404)
E/AndroidRuntime( 3014):    at android.view.View.measure(View.java:15367)
E/AndroidRuntime( 3014):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:607)
E/AndroidRuntime( 3014):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:420)
E/AndroidRuntime( 3014):    at android.view.View.measure(View.java:15367)
E/AndroidRuntime( 3014):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:607)
E/AndroidRuntime( 3014):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:420)
E/AndroidRuntime( 3014):    at android.view.View.measure(View.java:15367)
E/AndroidRuntime( 3014):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
E/AndroidRuntime( 3014):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
E/AndroidRuntime( 3014):    at android.view.View.measure(View.java:15367)
E/AndroidRuntime( 3014):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:849)
E/AndroidRuntime( 3014):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
E/AndroidRuntime( 3014):    at android.view.View.measure(View.java:15367)
E/AndroidRuntime( 3014):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
E/AndroidRuntime( 3014):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
E/AndroidRuntime( 3014):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2154)
E/AndroidRuntime( 3014):    at android.view.View.measure(View.java:15367)
E/AndroidRuntime( 3014):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1858)
E/AndroidRuntime( 3014):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1106)
E/AndroidRuntime( 3014):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1279)
E/AndroidRuntime( 3014):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
E/AndroidRuntime( 3014):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4223)
E/AndroidRuntime( 3014):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
E/AndroidRuntime( 3014):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
E/AndroidRuntime( 3014):    at android.view.Choreographer.doFrame(Choreographer.java:525)
E/AndroidRuntime( 3014):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
E/AndroidRuntime( 3014):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime( 3014):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 3014):    at android.os.Looper.loop(Looper.java:213)
E/AndroidRuntime( 3014):    at android.app.ActivityThread.main(ActivityThread.java:4786)
E/AndroidRuntime( 3014):    at java.lang.reflect.Method.invokeNative(Native Mehod)
E/AndroidRuntime( 3014):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 3014):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime( 3014):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime( 3014):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  610):   Force finishing activity tw.com.a_i_t.IPCamViewer/.MainActivity

The error of code is at (LocalFileBrowserFragment.java:950) , it is if(video_cursor.moveToFirst()){

It already get the Thumbnails of image and video file successfully.

Why this problem happened after I reload this page several time?

役に立ちましたか?

解決

I use the following code to solve the problem.

try {

                if(image_cursor.moveToFirst()){
                    long thumID = image_cursor.getLong(image_cursor.getColumnIndex("_ID"));
                    Bitmap bitmap_image = MediaStore.Images.Thumbnails.getThumbnail(getActivity().getContentResolver(), thumID, Images.Thumbnails.MICRO_KIND, null);
                    viewTag.mThumbnail.setImageBitmap(bitmap_image) ;

                }

                if(video_cursor.moveToFirst()){
                    long thumID = video_cursor.getLong(video_cursor.getColumnIndex("_ID"));
                    Bitmap bitmap_video = MediaStore.Video.Thumbnails.getThumbnail(getActivity().getContentResolver(), thumID, Video.Thumbnails.MICRO_KIND, null);
                    viewTag.mThumbnail.setImageBitmap(bitmap_video) ;               
                }

            } catch (NullPointerException e) {
                // TODO: handle exception
                Log.i(TAG, "cursor---NullPointerException");

                image_cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection_image, whereClause_image, null, null);
                video_cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection_video, whereClause_video, null, null);

            } finally{
                image_cursor.close();
                video_cursor.close();
            }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top