0

I'm having some trouble. I'm new to java and Android programming. I'm using a template to get started and I'm stuck.

I have a app that pulls images from my Tumblr Feed and presents them on the screen with a download button. It works fine but installs to the root of the internal storage. How do I save it to a folder in the internal storage called "/Pictures/Tumblr"?

My code is:

public void onLoadingComplete(final String imageUri, View view, Bitmap loadedImage) {
                spinner.setVisibility(View.GONE);
                // close button click event
                btnSave.setOnClickListener(new View.OnClickListener() {         
                    @Override
                    public void onClick(View v) {
                        String path = Environment.getExternalStorageDirectory().toString();
                        OutputStream fOut = null;
                        File file = new File(path, "tumblr_"+images.get(position).getId()+".jpg");
                        try {
                            fOut = new FileOutputStream(file);
                        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                        fOut.flush();
                        fOut.close();

                        MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());

                        String saved = getResources().getString(R.string.saved);
                        Toast.makeText(getBaseContext(), saved + " " + file.toString(), Toast.LENGTH_LONG).show();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });

I've tried changing the

File file = new File(path, "tumblr_"+images.get(position).getId()+".jpg");

To

File file = new File(path+"/Pictures/Tumblr", "tumblr_"+images.get(position).getId()+".jpg");

But I know it's wrong.

Can anyone help?

Kyle
  • 51
  • 3
  • What error you are getting ? – Hiren Patel Oct 10 '15 at 05:28
  • What do you mean "I know it's wrong" ? Apart from the fact that you should better use a path joining method (see https://stackoverflow.com/questions/711993/does-java-have-a-path-joining-method), and that you should make sure that the folder exists ? – personne3000 Oct 10 '15 at 05:30
  • 'folder in the internal storage called "/Pictures/Tumblr"'. Such a path does not exist and cannot exist. Did you have a look at the value of path in 'String path = Environment.getExternalStorageDirectory().toString();'. Maybe you can put your folders in that path. But not anymore with KitKat or later So give info. – greenapps Oct 10 '15 at 07:12
  • Further i see that you are -trying- to save your image to file and offer it also to the MediaStore. With that your image will twice appear on external storage. Is that what you want? – greenapps Oct 10 '15 at 07:16
  • 'But I know it's wrong.'. Which value does 'file.getAbsolutePath()' have in all those tries? – greenapps Oct 10 '15 at 07:18

0 Answers0