2014-07-11 50 views
0
package com.lociiapp; 

import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URL; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ImageView; 
import android.widget.Toast; 

import com.androidquery.AQuery; 
import com.example.imageslideshow.R; 

public class recciverfullimageActivty extends Activity { 
    String reccvierid; 
    Context context; 
    ImageView recciverimage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     Intent myintent = getIntent(); 
     reccvierid = myintent.getStringExtra("reccvierid"); 
     recciverimage = (ImageView) findViewById(R.id.recciverImage); 
     String myfinalpathare = reccvierid; 
     Toast.makeText(getApplicationContext(), reccvierid, 10000).show(); 
     String imagepathe = "http://api.lociiapp.com/TransientStorage/" 
       + myfinalpathare + ".jpg"; 

     try { 
      saveImage(imagepathe); 

      Log.e("****************************", "Sucess"); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    public void saveImage(String urlPath) throws Exception { 
     String fileName = "test.jpg"; 
     File folder = new File("/sdcard/LociiImages/"); 
     // have the object build the directory structure, if needed. 
     folder.mkdirs(); 

     final File output = new File(folder, fileName); 
     if (output.exists()) { 
      output.delete(); 
     } 

     InputStream stream = null; 
     FileOutputStream fos = null; 
     try { 

      URL url = new URL(urlPath); 
      stream = url.openConnection().getInputStream(); 
      // InputStreamReader reader = new InputStreamReader(stream); 
      DataInputStream dis = new DataInputStream(url.openConnection() 
        .getInputStream()); 
      byte[] fileData = new byte[url.openConnection().getContentLength()]; 
      for (int x = 0; x < fileData.length; x++) { // fill byte array with 
                 // bytes from the data 
                 // input stream 
       fileData[x] = dis.readByte(); 

      } 
      dis.close(); 
      fos = new FileOutputStream(output.getPath()); 
      fos.write(fileData); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (stream != null) { 
       try { 
        stream.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      if (fos != null) { 
       try { 
        fos.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 

} 

这是我的代码我试图保存图像来自服务器我们有图像的网址。当我运行此代码,然后文件夹在Sd卡中创建但图像不下载保存在Sd护理请帮助,并告诉我在哪里做错了。图像不保存到Android设备的Sd卡

+0

后logcat的。你有没有例外。还检查字符串imagepathe –

+0

你给写外部存储权限? –

+0

是的我alredy给 – Edge

回答

1

试试这个..

呼叫像下面而不是saveImage(imagepathe);

myAsyncTask myWebFetch = new myAsyncTask(); 
myWebFetch.execute(); 

myAsyncTask.class

class myAsyncTask extends AsyncTask<Void, Void, Void> { 

    public ProgressDialog dialog; 
    myAsyncTask() 
    { 
     dialog = new ProgressDialog(webview.this); 
     dialog.setMessage("Loading image..."); 
     dialog.setCancelable(true); 
     dialog.setIndeterminate(true); 
    } 
    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     dialog.dismiss(); 

    } 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialog.show(); 
    } 
    protected Void doInBackground(Void... arg0) { 
     try { 

      InputStream stream = null; 
      URL url = new URL("http://api.lociiapp.com/TransientStorage/286.jpg"); 
      URLConnection connection = url.openConnection(); 
      try { 
       HttpURLConnection httpConnection = (HttpURLConnection) connection; 
       httpConnection.setRequestMethod("GET"); 
       httpConnection.connect(); 

       File SDCardRoot = Environment.getExternalStorageDirectory(); 
       File myDir = new File(SDCardRoot + "/LociiImages"); 
       myDir.mkdirs(); 

       File file = new File(myDir,"test.jpg"); 

       FileOutputStream fileOutput = new FileOutputStream(file); 

       if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
        stream = httpConnection.getInputStream(); 
       } 

       byte[] buffer = new byte[1024]; 
       int bufferLength = 0; 

       while ((bufferLength = stream.read(buffer)) > 0) { 
        fileOutput.write(buffer, 0, bufferLength); 
       } 
       fileOutput.close(); 

      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

编辑

String imagePath = Environment.getExternalStorageDirectory().toString() + "/LociiImages/test.jpg"; 
Bitmap bitmap = BitmapFactory.decodeFile(imagePath); 
imageview.setImageBitmap(bitmap); 
+0

请在这里你保存图像请看看我的代码我已经应用了你的逻辑,但仍然不能保存文件夹创建,而在设备3g连接有 – Edge

+0

@Anil你可以发布该图像的网址。 – Hariharan

+0

http://api.lociiapp.com/TransientStorage/286.jpg – Edge

2

你的清单应该如下:

A.确保你有合适的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 


B.移动网络和文件IO逻辑到非UI线程

new AsyncTask<Params, Progress, Result>() { 
    @Override protected Result doInBackground() { 
     saveImage(imagepathe); 
    } 
    @Override protected void onPostExecute(String result) { 
     // update UI here 
    } 
}.execute(params); 


C.不要读的时候一个字节。这可能不是你的问题的根源,但它 确实让您的解决方案比它慢可能是:

相反的:

for(;;) { 
    fileData[x] = dis.readByte(); 
} 

这样做:

URL u = new URL(url); 
URLConnection connection = u.openConnection(); 
byte[] buffer = new byte[connection.getContentLength()]; 
stream.readFully(buffer); // <------------- read all at once 
stream.close(); 



D.最后,使用Picasso作业考虑

Picasso.with(context) 
    .load(url) 
    .resize(50, 50) 
    .centerCrop() 
    .into(imageView) 


现在你只是没有不需要写那么多代码来获得被你要去..

+0

conn.getContentLength()这一行什么conn在这里? – Edge

+0

看到编辑后 –

+0

我必须删除循环? – Edge