S
S
stilet28912014-06-11 05:13:12
Android
stilet2891, 2014-06-11 05:13:12

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

Download function
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;
    }
}

Saving is performed in a separate thread, taking a BitMap as input, the path to the picture is stored in sharedpreferences.
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

1 answer(s)
S
stilet2891, 2014-06-11
@stilet2891

Found the solution myself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question