0

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? (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?

Aakash
  • 5,181
  • 5
  • 20
  • 37
AtHul Antony
  • 77
  • 1
  • 11

1 Answers1

1
  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.

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

albeee
  • 1,452
  • 1
  • 12
  • 20
  • No permission requried for saving it locally. However you may have to use FileProvider file path while sharing your image in Android 6.0 and above. If you like the answer vote up. – albeee Feb 26 '17 at 13:56
  • your app must also grant the image receiver read permission to the downloaded file: ` intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);` – k3b Feb 28 '17 at 10:13