2015-05-22 75 views
-4

我試圖運行一個特定的方法時調用線程。像這樣:爲什麼我的應用程序在Android中運行Runnable線程時崩潰?

private void changeSize(final Bitmap image) { 
    Thread task = new Thread(new Runnable() { 
    @Override 
    public void run() { 
      byte[] image; 
      int width = image.getWidth(); 
      int height = image.getHeight(); 
      int newHeight = 0, newWidth = 0; 
      if (width > 350 || height > 350) { 
       if (width > height) { 
        newHeight = 350; 
        newWidth = (newHeight * width)/height; 
       } else { 
        nyBredden = 350; 
        newHeight = (newWidth * height)/width; 
       } 
      } else { 
       Toast.makeText(context, "test", Toast.LENGTH_LONG).show(); 
       } 
      Bitmap sizeChanged = Bitmap.createScaledBitmap(image, newWidth, newHeight, true); 
      if (sizeChanged != null) { 
       Toast.makeText(context, "test", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(context, "test", Toast.LENGTH_SHORT).show(); 
      } 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

      if (sizeChanged.getHeight() >= 350 || sizeChanged.getWidth() >= 350) { 
       sizeChanged.compress(Bitmap.CompressFormat.JPEG, 90, stream); 
      } else { 
       sizeChanged.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
      } 

      image = stream.toByteArray(); 
      if (image != null) { 
       myImage(image); //method 
       File path = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
       String namechanged = edMyType.getText().toString() + "_scalledDown" + ".jpg"; 

       File file = new File(path, namechanged); 
       FileOutputStream fos = null; 
       try { 
        fos = new FileOutputStream(file); 
        fos.write(image); 
        fos.flush(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } finally { 
        try { 
         if (fos != null) { 
          fos.close(); 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      } else { 
       Toast.makeText(context, "test", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
    }); 
    task.start(); 
} 

當我調用這個方法時,我的應用程序崩潰了。我試圖調試,但我不明白的變量,例如:

task: "Thread[Thread-149,5,main]" 
hasBeenStarted = false 

當我按下按鈕拍照時調用該方法。請任何人知道我做錯了什麼或者是否正確運行這樣的線程?

+5

發佈logcat輸出! –

+0

什麼是一些代碼? –

+0

發佈可運行內部的邏輯。 – Kartheek

回答

2

在後臺線程不能顯示Toast秒。

要麼移除吐司,要麼使用例如爲了調試的目的而進行日誌記錄,或者通過使用例如腳本將祝詞轉移到主UI線程。 ActivityrunOnUiThread()Handler

+0

但上下文就是有代碼的Activity。 – carl

+0

上下文無關緊要。敬酒只在UI線程上工作。 – laalto

+0

我嘗試沒有敬酒,我得到同樣的崩潰! – carl

-2
Thread splashThread = new Thread() { 
      public void run() { 
       synchronized (this) { 
        try { 
         wait(2000); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        startActivity(new Intent(getApplicationContext(), 
          MainActivity.class)); 
        finish(); 
       } 
      } 
     }; 
     splashThread.start(); 

嘗試使用我的代碼。我希望這能幫到您。

0

希望這有助於!

final Handler handler = new Handler(); 

      handler.postDelayed(new Runnable() { 

       @Override 
       public void run() 

       { 
         //do your stuff here ,but you canot update ui from any other thread 
} 
      }, 1000); 
+0

我現在正在顯示整個代碼。 – carl

1

你應該使用處理器...

private void changeSize(final Bitmap image) { 
Thread task = new Thread(new Runnable() { 
    @Override 
    public void run() { 

    } 
     private final Handler handler = new Handler() { 
      //some code here.. 
     }; 
}); 
task.start(); 
} 
相關問題