2017-02-26 31 views
0

在我的應用程序中,我必須從url下載一個圖像。我必須在我的應用程序中預覽該圖像,並且必須使用明確的意圖共享圖像。我面臨兩個問題如下:在Android中顯示圖像並共享到其他應用程序

  1. 我不想存儲在外部存儲的圖像。那麼除了共享首選還有哪些其他選項? (由於共享偏好對圖像進行解碼和編碼非常慢)。緩存是好的嗎?如果這很好,我該如何實現它?

  2. 位圖不能通過意圖顯式發送到其他應用程序(我正在談論我的應用程序中的共享圖像功能)。那麼,其他可能的選擇是什麼?

回答

1
  1. 下載圖像並將其存儲在緩存目錄

    URL url = new URL("YOUR_URL"); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
    connection.setDoInput(true); 
    connection.connect(); 
    InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); 
    String data1 = String.valueOf(String.format("/sdcard/dirname/%d.jpg",System.currentTimeMillis())); FileOutputStream stream = new FileOutputStream(data1); 
    ByteArrayOutputStream outstream = new ByteArrayOutputStream(); myBitmap.compress(Bitmap.CompressFormat.JPEG, 85, outstream); 
    byte[] byteArray = outstream.toByteArray(); stream.write(byteArray); 
    stream.close(); 
    
  2. 您需要在本地保存文件路徑URL跨應用程序共享。

    Intent intent = new Intent(Intent.ACTION_SEND); intent.setData(Uri.parse(file path)); startActivity(intent);

+0

這是否需要任何明確的許可? –

+0

未經許可需要在本地保存。但是,您可能必須在Android 6.0及更高版本中共享圖像時使用FileProvider文件路徑。如果你喜歡答案的話。 – albeee

+0

您的應用程序還必須將映像接收器讀取權限授予下載的文件:'intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);' – k3b

相關問題