从android库中选择图像时如何处理此错误?

时间:2011-11-04 06:54:56

标签: android multithreading android-layout canvas android-emulator

我在一个活动名称DrawingActivity中有来自android gallery的pick图像按钮。还有另一个活动名称为DrawingSurface。在DrawingSurface活动中,我使用线程绘制画布并处理它。

现在,当我要从图库中拾取图像时,我得到了这样的错误:

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)

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

我在DrawingSurface活动中遇到此错误的行如下:

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

我正在使用此代码在按下的按钮上选择图像:

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

所以现在可以告诉我如何处理这个错误吗?

感谢。

2 个答案:

答案 0 :(得分:1)

在开始使用线程之前,您应该使用thread.isAlive()

答案 1 :(得分:0)

试试这个或检查Thread对象是否为null?

第一种方式:

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);
        }
    }
}
第二种方式:

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