0

I'm quite new to android programming and I have the following problem. I want to be able to put an image om my server and then if I use my app it should use that image as a background. From previous research I understand I cant save any files to the drawable file? So is this even possible?

I am now this far:

  URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
  InputStream input = url.openStream();
  try { 
     String storagePath = Environment.getExternalStorageDirectory();
     OutputStream output = new FileOutputStream (storagePath + "/oranjelangb.png");
     try {
    byte[] buffer = new byte[1000000];
        int bytesRead = 0;
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
        output.write(buffer, 0, bytesRead);
        }
     } finally {
      output.close();
        }
    } finally {
   input.close();
 }

But I get the following error @ String storagePath = Environment.getExternalStorageDirectory(); The compiller says cannot convert file to string.

Michiel T
  • 537
  • 9
  • 23

1 Answers1

1

It should be possible. Simple steps may include :-

1) Download image file from server, Store it to SDcard or assets folder. links for step 1 >> link1 link2

2) Create a Bitmap from the file you downloaded.

3) Set that bitmap as a Background image.

You can pick steps and search on SO there should be lots of answers available.

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • I'll be able to do steps 2 and 3 with the current knowledge I have. But I dont know how to save the file to the SD or Assets folder. Can u please explain to my how? And if I store an image to the SD card wont the user be prompted every time it tries to store it? – Michiel T Jul 06 '12 at 11:30
  • 1
    Maybe this can help you: http://stackoverflow.com/questions/3296850/android-how-to-store-images-from-url-save-it-in-sd-card – Fran Verona Jul 06 '12 at 11:53
  • @Michiel T>> Fran Verona given an Excellent link for your question, I also edited my answer Please have a look. Thanks FranVerona – AAnkit Jul 06 '12 at 12:04
  • Thanks alot guys, it has helped alot but I get an error if i try to apply the piece of code. – Michiel T Jul 06 '12 at 13:28
  • I cant vote up because I dont have the required rep yet. And it still doenst work yet. Please try to help! – Michiel T Jul 06 '12 at 14:08
  • @Michiel Good, you gone so far, Now Accept the answer and Post your code as a different question. I have solution for file to string problem. but can not post it here, As your original question already answered. If you post it as a different question that "error in storing downloaded file", the can able to answer there. Post link of new question here – AAnkit Jul 06 '12 at 15:07
  • Okee thanks, I've posted the "new" question here: http://stackoverflow.com/questions/11365043/how-to-get-an-png-to-an-android-phone-external-sd – Michiel T Jul 06 '12 at 15:20