0

I have an Android app that downloads user's profile picture from a server but I don't know how to save it that only from my app can access it. What can I do?

Maddy
  • 4,525
  • 4
  • 33
  • 53
Behrooz Fard
  • 536
  • 4
  • 26
  • Have you tried this? https://stackoverflow.com/questions/18073260/save-load-image-to-from-local-storage#18073450 – Krzysztof Kubicki Nov 23 '17 at 11:06
  • I would recommend using serialization which is used to save data onto the device using variety of methods (I used text format) When you saved it, just retrieve it when you need the profile pic. – Nero Nov 23 '17 at 11:08
  • `that downloads user's profile picture`. That will be some kind of file like jpg or png i presume. Save the file in private internal memory. There no other app has access. Have a look at `getFilesDir()`. The place to store! – greenapps Nov 23 '17 at 11:16

3 Answers3

1

Most of the apps that save pictures are basically doing two things :

  1. Saving the picture as a file in the app's internal storage
  2. Save the URI pointing to that file in the app's preferences (or in a database if you have many of them)

You can check this link for the bitmap saving and this link for the retrieval of the URI

Arthur Attout
  • 2,701
  • 2
  • 26
  • 49
  • `Saving the picture as a bitmap (or another format if you wish) in the app's internal storage`. As a bitmap? Please show how you would save a picture as a bitmap to file. – greenapps Nov 23 '17 at 11:15
1

We have another option to store image in local. First convert your BITMAP image to base64 string using the following code.

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

Now you can store this base64 string in shared preferences or else you can use some database libraries like "ObjectBox" to store it as model class object.

Now reverse process..Base64 to bitmap conversion.

byte[] decodedString = Base64.decode(encoded, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
0

This link will help you to store as private data.

You can use the following code,

public void saveImage(Bitmap bitmap) {
         File imagePath = new File(Environment.getExternalStorageDirectory() + "/image.png");

        try {
            FileOutputStream fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }

Before that, convert the image into bitmap. And you should have the permission to access external storage first.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Use it in the Manifest file.

Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
  • External storage is accessable for all apps. Please read post again and see that OP does not want this. – greenapps Nov 23 '17 at 11:18