2017-10-20 190 views
-2

我有一个带有图片网址的字符串。Android:如何在Image-Url提供图片下载按钮并将其保存在内部存储器上时如何创建图片下载按钮

有人可以给我的代码来实现它吗?

以前完成的所有工作都不起作用。

代码到目前为止:

dlbtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       new DownloadImage().execute(id); 
      } 
     }); 

    public void saveImage(Context context, Bitmap b, String imageName) 
    { 
     FileOutputStream foStream; 
     try 
     { 
      foStream = context.openFileOutput(imageName, Context.MODE_PRIVATE); 
      b.compress(Bitmap.CompressFormat.PNG, 100, foStream); 
      foStream.close(); 
     } 
     catch (Exception e) 
     { 
      Log.d("saveImage", "Exception 2, Something went wrong!"); 
      e.printStackTrace(); 
     } 
    } 



    private class DownloadImage extends AsyncTask<String, Void, Bitmap> { 
     private String TAG = "DownloadImage"; 
     private Bitmap downloadImageBitmap(String sUrl) { 
      Bitmap bitmap = null; 
      try { 
       InputStream inputStream = new URL(sUrl).openStream(); // Download Image from URL 
       bitmap = BitmapFactory.decodeStream(inputStream);  // Decode Bitmap 
       inputStream.close(); 
      } catch (Exception e) { 
       Log.d(TAG, "Exception 1, Something went wrong!"); 
       e.printStackTrace(); 
      } 
      return bitmap; 
     } 

     @Override 
     protected Bitmap doInBackground(String... params) { 
      return downloadImageBitmap(params[0]); 
     } 

     protected void onPostExecute(Bitmap result) { 
      saveImage(getApplicationContext(), result, "my_image.png"); 
     } 
    } 

公共位图loadImageBitmap(上下文语境,字符串imageName){ 位图的位图= NULL; FileInputStream fiStream; try { fiStream = context.openFileInput(imageName); bitmap = BitmapFactory.decodeStream(fiStream); fiStream.close(); (例外e){ Log.d(“saveImage”,“Exception 3,Something went wrong!”); e.printStackTrace(); } return bitmap; }

但没有任何反应。 没有存储图像画廊,没有吐司将显示

+0

“所有什么香港专业教育学院之前完成不工作。”好的,你之前做了什么? – caesay

+0

我测试了一些脚本,但我无法得到它的工作 –

+0

好的,什么脚本?什么不起作用?我们不会为您编写代码,请告诉我们您已完成的工作以及您卡住的位置。只是说“它不工作”不会削减它。 – caesay

回答

相关问题