网络获取图片不错本地,进行展示





//引用的jar包

implementation files('libs/tbs_sdk_thirdapp_v3.2.0.1104_43200_sharewithdownload_withfilereader_withoutGame_obfs_20170609_115346.jar')
//依赖
compile 'com.lzy.net:okgo:2.1.4'
compile 'com.lzy.net:okserver:1.1.3'
compile 'pub.devrel:easypermissions:0.4.3'
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //初始化X5内核
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                //x5内核初始化完成回调接口,此接口回调并表示已经加载起来了x5,有可能特殊情况下x5内核加载失败,切换到系统内核。
            }

            @Override
            public void onViewInitFinished(boolean b) {
                //x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
                Log.e("@@","加载内核是否成功:"+b);
            }
        });
    }
}

 

public class MainActivity extends AppCompatActivity {

    private ImageView mImg;
    private Button mBut;
   // private String imgPATH = "http://pic25.nipic.com/20121205/10197997_003647426000_2.jpg";
    private String docUrl = "http://pic17.nipic.com/20111105/8323874_102621593000_2.jpg";
    private String download = Environment.getExternalStorageDirectory() + "/download/test/document/";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] perms = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(MainActivity.this, perms)) {
            EasyPermissions.requestPermissions(MainActivity.this, "需要访问手机存储权限!", 10086, perms);
        } else {
            //网络获取图片保存到本地
            initDoc();
        }
      mImg = findViewById(R.id.img);

    }

    private void initDoc() {
        int i = docUrl.lastIndexOf("/");
        String docName = docUrl.substring(i, docUrl.length());
        Log.d("print", "---substring---" + docName);

        //判断是否在本地/[下载/直接打开]
        File docFile = new File(download, docName);
        Log.d("print", ""+docFile);
        if (docFile.exists()) {

            Log.d("print", "本地存在");


        } else {
            OkGo.get(docUrl)//
                    .tag(this)//
                    .execute(new FileCallback(download, docName) {  //文件下载时,可以指定下载的文件目录和文件名
                        @Override
                        public void onSuccess(File file, Call call, Response response) {
                            // file 即为文件数据,文件保存在指定目录
                            Log.d("print", "下载文件成功");

                            Log.d("print", "" + file.getName());
                            //获取本地图片,进行展示
                             imgBitamp(file);

                        }

                        @Override
                        public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
                            //这里回调下载进度(该回调在主线程,可以直接更新ui)
                            Log.d("print", "总大小---" + totalSize + "---文件下载进度---" + progress);
                        }
                    });
        }

    }

    private void imgBitamp(File file) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(download+""+file.getName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Bitmap bitmap  = BitmapFactory.decodeStream(fis);
        Log.d("print1", "" + bitmap);
        mImg.setImageBitmap(bitmap);
       /* Bitmap qrCode = EncodingUtils.createQRCode(imgPATH,
                800, 800,
                BitmapFactory.decodeFile(String.valueOf(bitmap)));
        mImg.setImageBitmap(qrCode);*/

    }

}

 

 

你可能感兴趣的:(网络获取图片不错本地,进行展示)