android开发从URL加载图像

时间:2010-11-20 01:36:47

标签: android loading image

我知道这个问题已被提出并回答,但我遇到了另一个问题。我现在有一个应用程序,不会加载高于5mp但加载较小的图像。我不确定它是5mp还是超过1mb,我很确定它的mp虽然,我已经测试过了。如何加载各种尺寸的图片?我的代码如下,请帮助,请发布代码而不是链接,必须链接然后链接到可以帮助的代码。谢谢!

下载图片   @覆盖             protected Bitmap doInBackground(Object ... objects){               InputStream = null;                       BufferedInputStream bis = null;

                   Bitmap bmp = null;

                   try {
                    URL url = new URL(Pictureurl);
                       URLConnection conn = url.openConnection();
                     conn.connect();
                     is = conn.getInputStream();
                      bis = new BufferedInputStream(is);
                      bmp = BitmapFactory.decodeStream(bis);
                      return bmp;
                 } catch (MalformedURLException e) {
                  return null;
                 } catch (IOException e) {
                  return null;
                   }
    }
        @Override
        protected void onPostExecute(Object resizedBitmap) {
   MainPicture.setImageBitmap((Bitmap) resizedBitmap);
   connectionProgressDialog.dismiss();
         }

上传图片

public boolean SaveNewPicture(String SelectedFile,String UpdatedPhotoPath){

 HttpURLConnection conn = null;
 DataOutputStream dos = null;
 DataInputStream inStream = null;
 @SuppressWarnings("unused")

String Message = null;      //要上传到服务器的文件名的完整路径      String exsistingFileName = SelectedFile;

 String lineEnd = "\r\n";
 String twoHyphens = "--";
 String boundary = "*****";

 int bytesRead, bytesAvailable, bufferSize;

 byte[] buffer;

 int maxBufferSize = 1*1024*1024;

 @SuppressWarnings("unused")

String responseFromServer =“”;

 //Server path to the upload.php
 String urlString = "http://www.thelinkhookup.com/upload.php";

 try
 {
 //------------------ CLIENT REQUEST

 FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );

 // open a URL connection to the Servlet

 URL url = new URL(urlString);

 // Open a HTTP connection to the URL

 conn = (HttpURLConnection) url.openConnection();

 // Allow Inputs
 conn.setDoInput(true);

 // Allow Outputs
 conn.setDoOutput(true);

 // Don't use a cached copy.
 conn.setUseCaches(false);

 // Use a post method.
 conn.setRequestMethod("POST");

 conn.setRequestProperty("Connection", "Keep-Alive");

 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

 dos = new DataOutputStream( conn.getOutputStream() );

 dos.writeBytes(twoHyphens + boundary + lineEnd);
 dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
 dos.writeBytes(lineEnd);

 System.out.println("Headers are written");

 // create a buffer of maximum size

 bytesAvailable = fileInputStream.available();
 bufferSize = Math.min(bytesAvailable, maxBufferSize);
 buffer = new byte[bufferSize];

 // read file and write it into form...

 bytesRead = fileInputStream.read(buffer, 0, bufferSize);

 while (bytesRead > 0)
 {
 dos.write(buffer, 0, bufferSize);
 bytesAvailable = fileInputStream.available();
 bufferSize = Math.min(bytesAvailable, maxBufferSize);
 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
 }

 // send multipart form data necesssary after file data...

 dos.writeBytes(lineEnd);
 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

 // close streams
 System.out.println("File is written");
 fileInputStream.close();
 dos.flush();
 dos.close();

 }
 catch (MalformedURLException ex)
 {
 return false;
 }

 catch (IOException ioe)
 {
 Message = ioe.toString();
 return false;
 }

 //------------------ read the SERVER RESPONSE

 try {
 inStream = new DataInputStream ( conn.getInputStream() );
 String str;

 while (( str = inStream.readLine()) != null)
 {
 //the str contains the server response
 System.out.println("Server Response"+str);
 }
 inStream.close();
 //Message = "Should Have Worked";
 }

 catch (IOException ioex){

 return false;
 }
 try{
    HttpClient client = new DefaultHttpClient();
      HttpPost request = new HttpPost("http://thelinkhookup.com/UpdatePhotoName.php");
       List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
       postParameters.add(new BasicNameValuePair("Username", MyGlobalInfomation.getUsername()));
       postParameters.add(new BasicNameValuePair("Photopath", UpdatedPhotoPath));
       postParameters.add(new BasicNameValuePair("localphotopath",SelectedFile ));
       UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
       request.setEntity(formEntity);
       @SuppressWarnings("unused")

HttpResponse response = client.execute(request);            if(SelectedFile!= null || SelectedFile!=“”){            MyGlobalInfomation.setLocalPhotoPath(SelectedFile);            }       }            catch(例外e){             返回false; }      返回true;      }

1 个答案:

答案 0 :(得分:0)

你是在针对不同的网站尝试这个还是只针对一个?

我问的唯一原因是你提到了&gt;可能是1MB大小的原因。我知道一些网站托管网站为其成员提供免费网络空间,但每个网页(或任何其他下载)总共限制为1MB。如果成员想拥有真正的下载站点,那么他们必须为所涉及的额外带宽付费。

抱歉,它可能无法解答您的问题,但我认为您可能需要在怀疑您的代码之前检查它不是网站配置。