从网址保存图片

时间:2013-05-28 11:10:05

标签: android eclipse url

我想在我的代码中使用按钮保存URL中的图像我已经创建了目标文件夹,但是我尝试了各种代码来保存图片但是什么都不起作用:

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

请帮我修复此代码。

2 个答案:

答案 0 :(得分:5)

使用这种方式很容易:它在"sdcard/dhaval_files/"中保存文件。只需替换您的文件夹名称,并在android清单文件中授予权限write_external_storage

public void file_download(String uRl) {
        File direct = new File(Environment.getExternalStorageDirectory()
                + "/dhaval_files");

        if (!direct.exists()) {
            direct.mkdirs();
        }

        DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);

        Uri downloadUri = Uri.parse(uRl);
        DownloadManager.Request request = new DownloadManager.Request(
                downloadUri);

        request.setAllowedNetworkTypes(
                DownloadManager.Request.NETWORK_WIFI
                        | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false).setTitle("Demo")
                .setDescription("Something useful. No, really.")
                .setDestinationInExternalPublicDir("/dhaval_files", "test.jpg");

        mgr.enqueue(request);

    }

答案 1 :(得分:0)

您是否已将权限包含在清单文件中

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

试试这段代码

或者尝试这段代码,它会起作用

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