Answer the question
In order to leave comments, you need to log in
Can't upload picture android
I save the picture from the gallery to the application folder while creating a directory in it. When trying to load an image by Url, the BitmapFactory.decodeStream() function returns null.
Save function
private String saveImageToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(ProfileFragment.this.getActivity());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File carImagePath=new File(directory,"carImage.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(carImagePath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return carImagePath.getAbsolutePath();
}
private Bitmap loadImageFromStorage(String path)
{
if (path == null)
return null;
try {
File f = new File(path, "carImage.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
return b;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
return null;
}
}
class SaveImageTask extends AsyncTask<Bitmap, Void, String> {
@Override
protected String doInBackground(Bitmap... param)
{
return saveImageToInternalStorage(param[0]);
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
SharedPreferences sharedPrefLogin = ProfileFragment.this.getActivity().getSharedPreferences("TaxiPoltavaDriverLogin", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPrefLogin.edit();
editor.putString("carImage",result);
editor.commit();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question