将新图像上传到应用程序?

时间:2011-07-19 22:55:48

标签: android

在android中向应用程序显示不同图像的最佳方法是什么?

例如...... 我有一个展示不同图像的画廊。我想每周更改这些图像。现在我正在使用这个...

public class ImageAdapter extends BaseAdapter {
 /** The parent context */
   private Context myContext;

     /** URL-Strings to some remote images. */
    private String[] myRemoteImages = {
    "https://sites.google.com/site/theitrangers/images/modern_warfare_3_21027.jpg"
    ""
    };

     /** Simple Constructor saving the 'parent' context. */
      public ImageAdapter(Context c) { this.myContext = c; }

        /** Returns the amount of images we have defined. */
           public int getCount() { return this.myRemoteImages.length; }
        /* Use the array-Positions as unique IDs */
 public Object getItem(int position) { return position; }
     public long getItemId(int position) { return position; }

 /        ** Returns a new ImageView to
       * be displayed, depending on
         * the position passed. */
           public View getView(int position, View convertView, ViewGroup parent) {
          ImageView i = new ImageView(this.myContext);

            try {
            /* Open a new URL and get the InputStream to load data from it. */
            URL aURL = new URL(myRemoteImages[position]);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            /* Buffered is always good for a performance plus. */
            BufferedInputStream bis = new BufferedInputStream(is);
            /* Decode url-data to a bitmap. */
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
            /* Apply the Bitmap to the ImageView that will be returned. */
            i.setImageBitmap(bm);
    } catch (IOException e) {

            Log.e("DEBUGTAG", "Remtoe Image Exception", e);
    }

       /* Image should be scaled as width/height are set. */
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  / * Set the Width/Height of the ImageView. */
      i.setLayoutParams(new Gallery.LayoutParams(150, 150));
       return i;
    }


     public float getScale(boolean focused, int offset) {
     /* Formula: 1 / (2 ^ offset) */
     return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
   }
     }



}

所以在这个例子中我使用URL来获取图像。唯一的问题是我每次想要使用新图像时都会升级应用程序。正确吗?对我来说,最好的方法是什么?

2 个答案:

答案 0 :(得分:0)

我要做的是在Web服务器上安装一个服务器端脚本,告诉您的应用程序要显示哪些图像。

每次加载程序时,您都可以查询(HTTP GET)此脚本(您可以使用PHP或任何其他CGI),它将返回最新的图像。您可以直接返回整个图像,或者只是将应用程序的URL加载到其图像框架中。

答案 1 :(得分:0)

非常简单的答案:您可以将链接指向您控制的服务器上的静态URL(free tier ec2 server将是完美的),当您想要更改图像时,您可以覆盖文件URL指向。

同样hotlinking也不赞成,因为其他人正在为您的应用带宽使用付费