【发布时间】:2014-02-27 08:43:51
【问题描述】:
我想展示图片。我必须在服务中执行操作并在 UI 中显示结果。我不想根据要求使用异步任务。我不知道如何进行和显示图像。请帮帮我。
我的代码如下:
public class Service_Photo extends Service {
public Service_Photo() {
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
// For time consuming an long tasks you can launch a new thread here...
Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();
updateImages();
}
private void updateImages()
{
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "am/a.jpg";
File f = new File(baseDir + File.separator + fileName);
if(f.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
// img = (ImageView)findViewById(R.id.image);
// img.setImageBitmap(myBitmap);
Toast.makeText(getApplicationContext(), "Hello"+myBitmap.toString(), Toast.LENGTH_SHORT)
.show();
}
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
public class ImageDisplay extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//how to display the operation performed in service and display in image from service?
}
}
【问题讨论】:
-
startActivity() 将显示您的图像
-
抱歉找不到你。如何在 imageview 中显示?在哪里申报和领取? @pskink
-
检查如何从服务更新主线程,我猜它会解决你的问题。
-
@Shadow 创建一个在其 UI 中有一个 ImageView 的 Activity 并调用 setImageBitmap()
-
不,这意味着通过在活动中使用而不使用服务变得直截了当。 @pskink 我想从服务中使用。
标签: android android-layout android-service android-imageview android-file