android下载图片和视频到本地,求代码,求案例

android下载图片和视频到本地,求代码,求案例,第1张

下圆盯面是android多线程下载例子截图:

package mm.shandong.com.testmultithreaddownload

import android.app.ProgressDialog

import android.content.Intent

import android.net.Uri

import android.os.Environment

import android.os.Handler

import android.os.Message

import android.support.v7.app.AppCompatActivity

import android.os.Bundle

import android.text.TextUtils

import android.view.View

import android.widget.EditText

import android.widget.Toast

import java.io.File

import mm.shandong.com.testmultithreaddownload.thread.UpdateUIThread

import mm.shandong.com.testmultithreaddownload.util.FileUtil

public class TestMultiThreadDownLoadActivity extends AppCompatActivity {

    ProgressDialog pb

    EditText editText

    Handler handler

    private UpdateUIThread mUpdateUIThread = null

    ///文件路径

    private String url =

            "http://gdown.baidu.com/data/wisegame/3c00add7144d3915/kugouyinle.apk"

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_test_multi_thread_down_load)

        editText = (EditText) findViewById(R.id.editText)

        initHandler()

    }

    public void initHandler() {

        handler = new Handler() {

            @Override

            public void handleMessage(Message msg) {

                switch (msg.what) {

                    case FileUtil.startDownloadMeg:

                        pb.setMax(mUpdateUIThread.getFileSize())   //开始

                        break

                    case FileUtil.updateDownloadMeg:

                        if (!mUpdateUIThread.isCompleted())   //下载进度消息

                        {

                            pb.setProgress(mUpdateUIThread.getDownloadSize())

             槐尘               pb.setMessage("下载速度:" +

                               mUpdateUIThread.getDownloadSpeed() + "k/秒    " +

                               "   下载百分比" + mUpdateUIThread.getDownloadPercent() + "%")

                        } else {

                            pb.setMessage("下载完成")

                        }

                        break

                    case FileUtil.endDownloadMeg:

                        Toast.makeText(TestMultiThreadDownLoadActivity.this,

                                "下载完成,马上安装", Toast.LENGTH_SHORT).show()

                /*apk安装界面跳转*/

                        String filename = FileUtil.getFileName(url)

           橘明和             String str = "/myfile/" + filename

                        String fileName = Environment.getExternalStorageDirectory() + str

                        Intent intent = new Intent(Intent.ACTION_VIEW)

                        intent.setDataAndType(Uri.fromFile(new File(fileName)),

                                "application/vnd.android.package-archive")

                        startActivity(intent)

                        break

                }

                super.handleMessage(msg)

            }

        }

    }

   ///开始下载软件

    public void startDownLoad(View view) {

        if (TextUtils.isEmpty(editText.getText().toString())) {

            Toast.makeText(this, "你输入下载文件地址", Toast.LENGTH_SHORT).show()

            return

        } else {

            url = editText.getText().toString()

        }

        //启动线程初始化下载参数

        mUpdateUIThread = new UpdateUIThread(handler, url,

                FileUtil.setMkdir(this) + File.separator, FileUtil.getFileName(url))

        pb = new ProgressDialog(this)

        pb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)

        pb.show()

        mUpdateUIThread.start()

    }

}

例子代码太长,只能给你一部分,如果想看完整例子代码,请去360手机助手下载安卓学习手册,点击里面的源码可以查看完成代码,方面快速,里面还有安卓的其它小例子,共108个。

android源码目录下的build/envsetup.sh文件,描述编译的命令

-

m:

makes

from

the

top

of

the

tree.

-

mm:

builds

all

of

the

modules

in

the

current

directory.

-

mmm:

builds

all

of

the

modules

in

the

supplied

directories.

要想使用这些命令,首先需要在android源举租码根目录执行.

build/envsetup.sh

脚本设置环境

m:编译所有的模块

mm:编译当前目录下的模块,当前目录下要有android.mk文件

mmm:编译指定路径下的模块,指定路径下要有android.mk文件

下面举个例子说明,假设我要编译android下的\hardware\libhardware_legacy\power模块,当前目录为源码根目录,方法如下:

1、.

build/envsetup.sh

2、mmm

hardware/libhardware_legacy/power/

编译完后

运行

make

snod

会重毁答腊新将纤滑你改过的模块打入到system.img中

SDK目录下就有很多啊,几乎包含了所有方吵雹面的例子.

我用的2.1版SDK,例子就仿碰宽在samples\android-7下面,android-7就是2.1版的.

如果你用的其它版本就找到对应版本备亮就行了.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8213030.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存