【问题标题】:View Images From Url and Place in ImageView when User selects用户选择时从 URL 中查看图像并放置在 ImageView 中
【发布时间】:2012-06-06 07:33:25
【问题描述】:

我正在开发 Frame 应用程序。为此,我想从 url 显示图像(帧)。该网址有超过 50 张图片。为此,我使用 gridview,但它缺少一些要点,例如,

1.加载图片速度很慢。

2.我们在代码时声明图片的名称和大小,这样我们在发布应用程序后就不会在url中添加图片了。

我需要尽快解决这些问题。请任何人给我建议。

【问题讨论】:

  • 请告诉我你做了什么?
  • 我正在使用gridview。但需要解决方案而不是那个。从 url 显示图像并选择那个并在 imageview 中修复它

标签: android image url imageview


【解决方案1】:

使用下面的延迟加载列表视图链接,这将对您有所帮助。

Lazy Loading ListView

使用上面的链接代码并添加另一个活动和另一个布局来显示选定的图像,如果你有任何问题而不是告诉我,我会把完整的代码放在这里。

【讨论】:

  • 嗨 dipak,我浏览了惰性列表。但无法获取更改列表中图像大小的代码。你知道吗?
  • 请查看item.xml,将imageview 50dip的大小改为100dip或更大。
【解决方案2】:

1.加载图片速度很慢。

这将取决于您的带宽和设备缓存。

2。我们在代码时声明图片的名称和大小,这样我们发布应用后就不会在url中添加图片了。

您可以预先定义 URL,因此在代码时您可以将图像名称附加到 url。一旦您使用 AsyncTask 准备好 URL,就可以一张一张地下载图像 \

下面的 sn-ps 会对你有所帮助。

下载Helper.java

public interface DownloadHelper
{
    public void OnSucess(Bitmap bitmap);
    public void OnFailure(String response);
}

MainActivity.java

public class GalleryExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        DownloadHelper downloadHelper = new DownloadHelper()
        {
            @Override
            public void OnSucess(Bitmap bitmap)
            {
                ImageView imageView=(ImageView)findViewById(R.id.imageView);
                imageView.setImageBitmap(bitmap);
            }

            @Override
            public void OnFailure(String response)
            {
                Toast.makeText(context, response, Toast.LENGTH_LONG).show();
            }
        };
        new MyTask(this,downloadHelper).execute("image url");
    }

MyTask.java

public class DownloadTask extends AsyncTask<String, Integer, Object>
{
    private Context context;
    private DownloadHelper downloadHelper;
    private ProgressDialog dialog;


    public DownloadTask(Context context,DownloadHelper downloadHelper)
    {
        this.context = context;

    }

    @Override
    protected void onPreExecute()
    {
        dialog = new ProgressDialog(context);
        dialog.setTitle("Please Wait");
        dialog.setMessage("Fetching Data!!");
        dialog.setCancelable(false);
        dialog.show();
        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(String... params)
    {
        URL aURL = new URL(myRemoteImages[position]);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();

        BufferedInputStream bis = new BufferedInputStream(is);
        /* Decode url-data to a bitmap. */
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
        return bm;
    }

    @Override
    protected void onPostExecute(Object result)
    {
        dialog.dismiss();
        if (result != null)
        {
            downloadHelper.OnSucess((Bitmap)result);
        } 
        else
        {
            downloadHelper.OnFailure("Error in Downloading Data!!");
        }
        super.onPostExecute(result);
    }
}

【讨论】:

  • 感谢您的回答。但我需要按用户在 url 中选择图像。该 url 有更多图像,用户选择一个,我需要在 imageview 中显示该图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2013-12-29
  • 2013-06-19
  • 2023-03-29
相关资源
最近更新 更多