我试图运行一个特定的方法时调用线程。像这样:为什么我的应用程序在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
当我按下按钮拍照时调用该方法。请任何人知道我做错了什么或者是否正确运行这样的线程?
发布logcat输出! –
什么是一些代码? –
发布可运行内部的逻辑。 – Kartheek