2

I want to save an image from a URL with a button in my code i have create the destination folder, but I have tried various codes to save a picture but do not work nothing :

public class B_X extends Activity {   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bx);

        Button buttonSetWallpaper = (Button)findViewById(R.id.bSetWall);
        buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                File folder = new File(Environment.getExternalStorageDirectory() + "/PerfectAss/");
                boolean success = false;
                if (!folder.exists()) {
                    success = folder.mkdirs();
                }

                if (!success) {
            } else {
            }

                 Toast.makeText(getApplicationContext(), "The image has been saved", Toast.LENGTH_LONG).show();
                    URL url = new URL ("http://bitsparrow.altervista.org/wp-content/uploads/2013/04/5.jpg");
                    InputStream input = url.openStream();
                     try {

                       File storagePath = Environment.getExternalStorageDirectory();
                       OutputStream output = new FileOutputStream (storagePath + "/myImage.png");
                       try {
                            byte[] buffer = new byte[1500];
                             int bytesRead = 0;
                             while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                             output.write(buffer, 0, bytesRead);
                             }
                             } finally {
                               output.close();
                              }
                              } finally {
                              input.close();
                              }

Please help me fix this code.

Gioele
  • 83
  • 1
  • 11
  • if u were search google for same question u'll get 100 link. – NaserShaikh May 28 '13 at 11:14
  • 1
    So what have you actually tried? Because the code above shows nothing that has to do with downloading an image and saving it. – Sander van't Veer May 28 '13 at 11:14
  • you have done NOTHING about download the image, you just checked if the Directory (PerfectAss) exists and created it if not. Search for get image from url on android. PS: WHAT THE HELL your app will do? What kind of name u put on the image directory. Remember about Google Play Content Policy – JannGabriel May 28 '13 at 11:20
  • I edited the question and I entered the code I used – Gioele May 28 '13 at 11:30

1 Answers1

0

have you include permission into your manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And try this code

Or then try this code,it will work

URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();

try {
    //The sdcard directory e.g. '/sdcard' can be used directly, or 
    //more safely abstracted with getExternalStorageDirectory()
    File storagePath = Environment.getExternalStorageDirectory();
    OutputStream output = new FileOutputStream (storagePath + "/myImage.png");

    try {
        byte[] buffer = new byte[aReasonableSize];
        int bytesRead = 0;

        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
            output.write(buffer, 0, bytesRead);
        }
    } finally { output.close(); }
} finally { input.close(); }
sschrass
  • 7,014
  • 6
  • 43
  • 62
Zeeshan Chaudhry
  • 842
  • 2
  • 15
  • 31
  • okay try that above code and fer details check this link http://android-example-code.blogspot.in/p/download-store-and-read-images-from.html – Zeeshan Chaudhry May 28 '13 at 11:21
  • URL url = new URL ("http://bitsparrow.altervista.org/wp-content/uploads/2013/04/5.jpg"); eclipse tells me Description Resource Path Location Type Unhandled exception type MalformedURLException B_X.java – Gioele May 28 '13 at 11:26