2016-08-23 75 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,如果在片段中 –