简体   繁体   English

显示图像并与Android中的其他应用共享

[英]Displaying image and sharing to other apps in Android

In my app, i have to download one image from the url. 在我的应用中,我必须从网址下载一张图片。 I have to preview that image in my app and i have to share the image using explicit intent. 我必须在我的应用程序中预览该图像,并且必须使用显式意图共享该图像。 I have faced two issues as follows: 我面临以下两个问题:

  1. I don't want to store the image in external storage. 我不想将图像存储在外部存储中。 So what are the other options than sharedpreferences? 那么,除了sharedpreferences之外,还有哪些其他选项? (Since shared preference is very slow to decode and encode image). (由于共享首选项非常慢,无法对图像进行解码和编码)。 Is caching is good? 缓存好吗? If it is good how can i achieve it? 如果很好,我该如何实现?

  2. Bitmaps cannot be send explicitly to other apps through intents.(I am talking about share image feature in my app). 无法通过意图将位图显式发送到其他应用程序。(我正在谈论我的应用程序中的共享图像功能)。 So what are the other possible options for that? 那么,还有哪些其他可能的选择呢?

  1. Download image and store it in your cache directory 下载图像并将其存储在缓存目录中

     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. You need locally saved file path URL to share across apps. 您需要本地保存的文件路径URL才能在应用之间共享。

    Intent intent = new Intent(Intent.ACTION_SEND); 意图意图=新意图(Intent.ACTION_SEND); intent.setData(Uri.parse(file path)); intent.setData(Uri.parse(文件路径)); startActivity(intent); startActivity(intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM