Behrooz Fard
Behrooz Fard

Reputation: 555

Save profile picture in Android application

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?

Upvotes: 0

Views: 3306

Answers (3)

RaviTejaSiddabattuni
RaviTejaSiddabattuni

Reputation: 156

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);

Upvotes: 1

Arthur Attout
Arthur Attout

Reputation: 2926

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

Upvotes: 1

Jyoti JK
Jyoti JK

Reputation: 2171

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.

Upvotes: 0

Related Questions