Вы можете сохранить изображение во внутреннем хранилище, так как ему не требуется разрешение хранилища для загрузки изображения, которое вы можете использовать
private void loadImage(String path){
try {
File file = new File(path, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(file));
ImageView img=(ImageView)findViewById(R.id.imgPicker);
img.setImageBitmap(b); // it will display the image in imageview
String file_path = saveToInternalStorage(b); // store this file_path in db
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
, для загрузки изображения с сервера вы можете использовать библиотеку glideнапример,
GlideApp.with(context)
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
@Override public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
String file_path = saveToInternalStorage(resource); // store the bitmap
// save this file_path to your db
}
});
для сохранения изображения, вы можете использовать этот метод
private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return mypath.getAbsolutePath();
}
для загрузки растрового изображения, вы можете использовать glide для получения растрового изображения из glide и использовать метод save для сохранения растрового изображения.после сохранения файла образа во внутреннем хранилище сохраните путь в вашей базе данных, надеюсь, он вам поможет ...