Reputation: 555
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
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
Reputation: 2926
Most of the apps that save pictures are basically doing two things :
You can check this link for the bitmap saving and this link for the retrieval of the URI
Upvotes: 1
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