尝试使用Picasso在外部存储中保存图像

时间:2017-06-30 07:31:18

标签: android picasso

我正在尝试从URL获取图像以供显示,并且还希望存储在我们手机的外部存储中。我没有收到错误但是图像没有保存在磁盘中,为什么?我没有得到。这可能是一个非常小的问题,但我是制作Android应用程序的新手,所以请给我一个建议我应该做什么。我这两次给毕加索打电话......

Picasso.with(this).load(currentUrl)
//.error(R.drawable.error_detail)
.into(imageView, new EmptyCallback() {
    @Override public void onSuccess() {
        progressBar.setVisibility(View.GONE);
    }
    @Override
    public void onError() {
        progressBar.setVisibility(View.GONE);
    }
});

Picasso.with(this)
        .load(currentUrl)
        .into(target);

目标类就是这样---->

private Target target = new Target() {

@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() +"/image.jpg");
            try
            {
                file.createNewFile();
                FileOutputStream ostream = new FileOutputStream(file);
                bitmap.compress(CompressFormat.JPEG, 75, ostream);
                ostream.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
    if (placeHolderDrawable != null) {
    }
}
};

1 个答案:

答案 0 :(得分:0)

要求获得运行时间许可

ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

检查文件 https://developer.android.com/training/permissions/requesting.html