【问题标题】:Android HttpRequest Image downloadAndroid HttpRequest 图片下载
【发布时间】:2013-03-10 14:21:54
【问题描述】:

除了应用程序,我没有收到任何错误。什么都不做。

下载链接http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device

package com.androidhive.httprequests;

import java.io.IOException;
import java.net.URL;

import android.util.Log;

import com.androidhive.httprequests.AndroidHTTPRequestsActivity;

public class DownloadFromUrl {

public static void main ()
{
    try {
    AndroidHTTPRequestsActivity hallo = new AndroidHTTPRequestsActivity();
    String imageURL2 = "sample.jpg";
    URL url = new URL("http://api.androidhive.info/images/" + imageURL2);
    String filename2 = String.valueOf(url.hashCode());
    hallo.DownloadFromUrl(imageURL2, filename2);
    } catch (IOException e) {
        Log.d("ImageManager", "Error: " + e);
}
}

【问题讨论】:

  • 请发布 logcat 日志。您遇到什么错误?
  • 您是否更新了 AndroidManifest.xml 以获得 Internet 权限?
  • 是的,问题是我没有收到任何错误
  • 好吧,您发布的代码不是来自 Android 应用程序。这是一个简单的 Java 应用程序。
  • 我如何在 Android 中做到这一点?

标签: android html image load httprequest


【解决方案1】:

基本的下载示例代码应该是这样的:

private class DownloadWallpaperTask extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        String wallpaperURLStr = params[0];
        String localPath = Integer.toString(wallpaperURLStr.hashCode());
        try {
            URL wallpaperURL = new URL(wallpaperURLStr);
            URLConnection connection = wallpaperURL.openConnection();

            //get file length
            int filesize = connection.getContentLength();
            if(filesize < 0) {
                downloadProgressDialog.setMax(1000000);
            } else {
                downloadProgressDialog.setMax(filesize);
            }

            InputStream inputStream = new BufferedInputStream(wallpaperURL.openStream(), 10240);
            String appName = getResources().getString(R.string.app_name);
            OutputStream outputStream = openFileOutput(localPath, Context.MODE_PRIVATE);
            byte buffer[] = new byte[1024];
            int dataSize;
            int loadedSize = 0;
            while ((dataSize = inputStream.read(buffer)) != -1) {
                loadedSize += dataSize;
                publishProgress(loadedSize);
                outputStream.write(buffer, 0, dataSize);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return localPath;
    }


    protected void onProgressUpdate(Integer... progress) {
        downloadProgressDialog.setProgress(progress[0]);
    }

    protected void onPostExecute(String result) {
        downloadProgressDialog.hide();

        //open preview activity
        Bundle postInfo = new Bundle();
        postInfo.putString("localpath", result);

        if (previewPanelIntent == null) {
            previewPanelIntent = new Intent(MainActivity.this,
                    PreviewPanel.class);
        }

        previewPanelIntent.putExtras(postInfo);
        startActivity(previewPanelIntent);
    }
}

为了获取更多信息,我在我的应用中使用了一个 android 应用源代码:

Android Download Image with Progress Bar

【讨论】:

    猜你喜欢
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多