How to handle this error while select image from android gallery?

不羁的心 提交于 2019-12-08 13:14:52

问题


I have button of pick image from android gallery in one activity name DrawingActivity. and there is another activity name as DrawingSurface. In DrawingSurface activity i am using thread to draw the canvas and handle it.

Now while i am going to pickup the Image from gallery i got error like this:

11-04 12:12:50.226: ERROR/AndroidRuntime(518): FATAL EXCEPTION: main
11-04 12:12:50.226: ERROR/AndroidRuntime(518): java.lang.IllegalThreadStateException: Thread already started.
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at java.lang.Thread.start(Thread.java:1322)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at com.example.drawing.DrawingSurface.surfaceCreated(DrawingSurface.java:106)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.view.SurfaceView.updateWindow(SurfaceView.java:532)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:206)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.view.View.dispatchWindowVisibilityChanged(View.java:3891)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.view.ViewRoot.performTraversals(ViewRoot.java:744)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.os.Looper.loop(Looper.java:123)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at java.lang.reflect.Method.invoke(Method.java:521)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-04 12:12:50.226: ERROR/AndroidRuntime(518):     at dalvik.system.NativeStart.main(Native Method)

==========================

The Line where i got this error on DrawingSurface activity is as below:

 public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
        thread.setRunning(true);
        thread.start(); // error at this line
}

I am using this code to pick image on button pressed:

Intent intent = new Intent(); 
            intent.setType("image/*"); 
            intent.setAction(Intent.ACTION_GET_CONTENT);// 
            //startActivity(intent);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
            //finish();

So Now can anybudy tell me how i can handle this error ?

Thanks.


回答1:


You should use thread.isAlive() before starting the Thread .




回答2:


Try this or check Thread object null or not?

first way:

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
        }
    }
}

second way :

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
        thread.setRunning(true);
        if(!thread.isAlive())  
         thread.start(); // error at this line
}


来源:https://stackoverflow.com/questions/8005811/how-to-handle-this-error-while-select-image-from-android-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!