1

当我运行此代码时,有时会出现错误,但并非总是如此。我看不到我的错误。

调用代码:

private void processPhotoUpdate(File tempFile)
        {
            Log.i(TAG, "Made it into Process Photo Update" );
            ProcessProfilePhotoTask task = new ProcessProfilePhotoTask()
                {
                    @Override
                    protected void onPostExecute(Bitmap result)
                        {
                            Log.i(TAG, "Not Null");
                            if (result != null){                    
                            Log.i(TAG, "on Post Execute");
                            android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(result.getWidth(), result.getHeight());
                            params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
                            photo.setLayoutParams(params);
                            photo.setImageBitmap(result);
                            }
                        }
                };                  
                    task.execute(tempFile);
        }

我打电话的课程是:

public class ProcessProfilePhotoTask extends AsyncTask<File, ProcessProfilePhotoTask.ProcessingState, Bitmap> {


public enum ProcessingState {
    STARTING,
    PROCESSING_LARGE,
    FINISHED
}


public static final String TAG = "ProcessProfilePhotoTask";

public ProcessProfilePhotoTask() {
    super();
}

@Override
protected Bitmap doInBackground(File... files) {
    ProcessingState[] s = new ProcessingState[1];
    //BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    if (files.length != 1) {
        throw new IllegalArgumentException("We expect to process only one file");
    }
    try {
        s[0] = ProcessingState.PROCESSING_LARGE;
        publishProgress(s);

        Bitmap largePhoto = BitmapFactory.decodeStream(new FileInputStream(files[0]));
        int height = largePhoto.getHeight();
        int width = largePhoto.getWidth();
        int density = largePhoto.getDensity();
        Log.d(TAG,"large image processing "+ height+"x"+width+"den="+density+"type=");

        return largePhoto;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
}

LogCat 是。

12-13 15:41:02.064: D/skia(14099): --- SkImageDecoder::Factory returned null
12-13 15:41:02.064: W/dalvikvm(14099): threadid=9: thread exiting with uncaught             exception (group=0x400259f8)
12-13 15:41:02.074: E/AndroidRuntime(14099): FATAL EXCEPTION: AsyncTask #1
12-13 15:41:02.074: E/AndroidRuntime(14099): java.lang.RuntimeException: An error     occured while executing doInBackground()
12-13 15:41:02.074: E/AndroidRuntime(14099):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at     java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at java.lang.Thread.run(Thread.java:1102)
12-13 15:41:02.074: E/AndroidRuntime(14099): Caused by: java.lang.NullPointerException
12-13 15:41:02.074: E/AndroidRuntime(14099):    at com.androidworks.ProcessProfilePhotoTask.doInBackground(ProcessProfilePhotoTask.java:57)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at com.androidworks.ProcessProfilePhotoTask.doInBackground(ProcessProfilePhotoTask.java:1)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-13 15:41:02.074: E/AndroidRuntime(14099):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-13 15:41:02.074: E/AndroidRuntime(14099):    ... 4 more
12-13 15:41:04.014: I/Process(14099): Sending signal. PID: 14099 SIG: 9
4

1 回答 1

0

通过在 doInBackgroud() 中添加此条件来尝试此操作....

if(null != largePhoto)
{
    // your code here
}
else
{
   // error code here
}
于 2012-12-13T05:56:41.780 回答