无法在Picaso中加载目标

时间:2018-08-18 10:45:46

标签: java android resize picasso target

我正在尝试通过picaso加载网址。我想使用调整大小来加载图像并在加载后保存图像。我试图使用target并使用picaso加载它,但不会调用onPrepareLoad方法。跳转到onBitmapLoaded,看不到任何加载的图像。

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_view);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Get intent data
    Intent i = getIntent();

     imageView = (ImageView) findViewById(R.id.SingleView);

    if (i.getBooleanExtra("isGallery", false)) {

        // Selected image id
        position = i.getExtras().getInt("id");

        FEED_URL = getString(R.string.url) + "photogallery/" + i.getStringExtra("category") + "/" + i.getStringExtra("image");

        imageView.setTag(target);
        loadImage();

    }

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (ContextCompat.checkSelfPermission(ImageViewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(ImageViewActivity.this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_STORAGE);
            } else {

                 share("zoho",mFile);

            }
        }
    });
}
private Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
        try {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
            ostream.flush();
            ostream.close();

            mFile = file;

        } catch (IOException e) {
            Log.e("IOException", e.getLocalizedMessage());
        }

    }

    @Override
    public void onBitmapFailed(Exception e, Drawable errorDrawable) {
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

        imageView.setImageDrawable(placeHolderDrawable);
    }
};

public void loadImage() {
    Picasso.get().load(FEED_URL).resize(400,400).into(target);
}

@Override
public void onDestroy() {  // could be in onPause or onStop
    Picasso.get().cancelRequest(target);
    super.onDestroy();
}

这里有什么问题?请帮忙。谢谢你。

0 个答案:

没有答案