Android中内存泄漏的问题
PHPz
PHPz 2017-04-17 17:42:18
0
1
722

我们的项目需要一个 压缩图片后上传到指定地址并返回url 的功能,Activity需要获取这个url做一些处理。我写的代码大概是这样的

/**
 * url是服务器地址,img_path是文件本地路径,Action是自己写的一个接口
 */
    public void UploadImg(final String url, final String img_path, final Action action) {
        Luban.get(context)//这个context是Application
                .load(new File(img_path))
                .asObservable()
                .timeout(10, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .subscribe(subscriber = new Subscriber<File>() {
                    @Override
                    public void onCompleted() {}

                    @Override
                    public void onError(Throwable e) {
                        action.onError(e, new File(img_path));
                    }

                    @Override
                    public void onNext(final File file) {
                        //原项目用xutils上传图片
                        RequestParams params = new RequestParams();
                        params.addBodyParameter("img", file);
                        HttpUtils http = new HttpUtils(10000);
                        http.configTimeout(10000)
                                .configCurrentHttpCacheExpiry(10000)
                                .send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
                                    @Override
                                    public void onStart() {}

                                    @Override
                                    public void onLoading(long total, long current, boolean isUploading) {}

                                    @Override
                                    public void onSuccess(ResponseInfo<String> responseInfo) {
                                        Log.i(TAG, "http onSuccess: " + Thread.currentThread().getName());
                                        action.onSuccess(responseInfo.result, file);
                                    }

                                    @Override
                                    public void onFailure(HttpException error, String msg) {
                                        error.printStackTrace();
                                        action.onError(error, file);
                                    }
                                });
                    }
                });
    }

activity中是这样的

            BitmapUtils.getInstance().UploadImg(APIService.URL_SEND_PIC, IMG_PATH_FLAG, new BitmapUtils.Action() {
                @Override
                public void onStart() {
                    showProgressDialog();
                }

                @Override
                public void onUpdate(int total, int current) {
                }

                @Override
                public void onSuccess(String json, File img) {
                    setImgInfo(json);//需要对Activity中一些变量进行操作
                }

                @Override
                public void onError(Throwable t, File img) {
                    cacheImg(img);//涉及到数据库操作...
                }
            });

在这个回调里引起了很严重的内存泄漏

请问有什么办法能避免吗,或者直接改写也行

PHPz
PHPz

学习是最好的投资!

membalas semua(1)
Peter_Zhu

我用的是这个压缩图片https://github.com/Curzibn/Luban
配合rxjava然后上传,我也用了LeakCanary,没有内存泄漏

/**
 * 上传图片
 *
 * @param context
 * @param fileUrl
 * @throws FileNotFoundException
 */

public static void upLoadFile(final Context context, final String fileUrl, Observer<ImageGet> observer) {
    final ApiService apiservice = Api.createApiService();
    Uri uri = Uri.parse(fileUrl);
    final File file = new File(uri.getPath());
    Luban.get(context)
            .load(file)
            .putGear(Luban.THIRD_GEAR)
            .asObservable()
            .doOnError(new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                    throwable.printStackTrace();
                }
            })
            .onErrorResumeNext(new Func1<Throwable, Observable<? extends File>>() {
                @Override
                public Observable<? extends File> call(Throwable throwable) {
                    return Observable.empty();
                }
            })
            .flatMap(new Func1<File, Observable<ImageGet>>() {
                @Override
                public Observable<ImageGet> call(File file) {
                    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                    MultipartBody.Part picturePart = MultipartBody.Part.createFormData("Filedata", file.getName(), requestFile);
                    return apiservice.postImage(UserUtil.getUser(context).getToken(), picturePart);
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(observer);
}
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan