2014-12-29 76 views
-2
public class MainActivity extends Activity { 
    LinearLayout linearMain; 
    CheckBox checkBox; 

    ImageView imageview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     imageview = (ImageView) findViewById(R.id.image); 
     new myAsyncTask().execute(); 

    } 

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

     public ProgressDialog dialog; 

     myAsyncTask() { 
      dialog = new ProgressDialog(MainActivity.this); 
      dialog.setMessage("Loading image..."); 
      dialog.setCancelable(true); 
      dialog.setIndeterminate(true); 
     } 

     // AQuery aq = new AQuery(context); 
     // aq.id(recciverimage).image(bitmap); 
     // ; 

     @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/" 
         + "1" + ".jpg"); 
       URLConnection connection = url.openConnection(); 
       try { 
        // The sdcard directory e.g. '/sdcard' can be used directly, 
        // or 
        // more safely abstracted with getExternalStorageDirectory() 
        File storagePath = Environment 
          .getExternalStorageDirectory(); 
        OutputStream output = new FileOutputStream(new File(
          storagePath, "1.jpg")); 
        try { 
         byte[] buffer = new byte[1024]; 
         int bytesRead = 0; 
         while ((bytesRead = stream.read(buffer, 0, 
           buffer.length)) >= 0) { 
          output.write(buffer, 0, bytesRead); 
         } 
        } finally { 
         output.close(); 
        } 

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

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

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 

      dialog.dismiss(); 
      String imagePath = Environment.getExternalStorageDirectory() 
        .toString() + "/LociiImages/" + "1" + ".jpg"; 
      Bitmap bitmap = BitmapFactory.decodeFile(imagePath); 

      AQuery aq = new AQuery(MainActivity.this); 
      aq.id(imageview).image(bitmap); 

      // recciverimage.setImageBitmap(bitmap); 

     } 

    } 
} 

这是我的代码从我在SD卡,并从SD卡下载图像从服务器和存储我们打印图像视图http://api.lociiapp.com/TransientStorage/1.jpg图像使用这URL正在下载图像到服务器,但无法存储,请建议我在哪里做错了它给空指针异常。空指针异常图像并存储到SD卡中的Android

+0

你的logcat在哪里? –

+0

logcat ??????????? –

+0

当我调试然后catchblock执行 – Departure

回答

2

我认为这个问题是在这里:

OutputStream output = new FileOutputStream(new File(
         storagePath, "1.jpg")); 

在您storagePath是:

File storagePath = Environment 
         .getExternalStorageDirectory(); 

,后来您尝试访问下载图片:

String imagePath = Environment.getExternalStorageDirectory() 
       .toString() + "/LociiImages/" + "1" + ".jpg"; 

如果您比较存储路径和你正在寻找的路径,然后你会看到你是大海在子文件夹LociiImages中切入,但您没有将图片保存在那里。

这就是为什么这条线:

Bitmap bitmap = BitmapFactory.decodeFile(imagePath); 

抛出一个NPE。只是一个建议。希望能帮助到你。

+0

下载图像到SD卡现在文件保存,但大小为0B – Departure

+0

@出发没有办法。将完整的Logcat发布到问题的底部! – Mike

+0

没有错误即将到来没有locat http://api.lociiapp.com/TransientStorage/1.jpg我只想下载此图像并存储在SD卡 – Departure