2016-08-23 72 views
0

我想显示图像从服务器的特定id如果图像存在。如果没有图像的id,我想显示一些默认图像,然后我想从相机捕捉图像,剪裁它,然后将该图像加载到服务器,并且我想在第一个活动中显示该加载的图像。如何才能做到这一点。我尝试了一些代码,但如果服务器中没有图像,它会显示一个默认图像,但它不会被第一次上传的图像取代。如果我再次登录,它将显示。如何解决这个问题?用android上传的图像替换默认图像

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_student_details); 
      { 
      String IMAGE_URL = "http://xxx.xxx.xx.x/mobile_app/" + img_path + "/" + img_name; 
      new DownloadImageTask((ImageView) findViewById(R.id.ProfilePicIV)) 
        .execute(IMAGE_URL); 
      } 


    public void onResume() 
     { // After a pause OR at startup 
      super.onResume(); 
      getIntent(); 
      String auid_num = auid_s.getText().toString(); 
      String imei_num = imei_s.getText().toString(); 
      String img_name = getIntent().getStringExtra("img_name"); 
      String img_path = getIntent().getStringExtra("img_path"); 
      //Refresh your stuff here 
      SendDataToServer(auid_num, imei_num); 
      String IMAGE_URL = "http://xxx.xxx.xx.x/mobile_app/" + img_path + "/" + img_name; 
      new DownloadImageTask((ImageView) findViewById(R.id.ProfilePicIV)) 
        .execute(IMAGE_URL); 

     } 

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
     ImageView bmImage; 

     public DownloadImageTask(ImageView bmImage) { 
      this.bmImage = bmImage; 
     } 

     protected Bitmap doInBackground(String... urls) { 
      String urldisplay = urls[0]; 
      Bitmap mIcon11 = null; 
      try { 
       InputStream in = new java.net.URL(urldisplay).openStream(); 
       mIcon11 = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return mIcon11; 
     } 

     protected void onPostExecute(Bitmap result) { 
      bmImage.setImageBitmap(result); 
     } 
    } 

xml: 
<ImageView 
       android:id="@+id/ProfilePicIV" 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:background="@drawable/default_image" 
       android:scaleType="fitXY" /> 
+0

哪里是你的'DownloadImageTask'方法? –

回答

0

只需使用毕加索图书馆来显示您的图像并使用您的代码来下载它。 在你的build.gradle

compile 'com.squareup.picasso:picasso:2.5.2' 

添加这一点,并使用

Picasso.with(context).load(uri).error(R.drawable.default_image).into(imageview); 
+0

我试图通过替换新的DownloadImageTask((ImageView)findViewById(R.id.ProfilePicIV)).execute(IMAGE_URL);但它不起作用 – siri

+0

它给出错误,因为上下文不能为空 – siri

+0

如果在活动中使用getApplicationContext()( 或getActivity,如果在片段中 –