Android : Load image with Picasso onBitMapLoaded not called

泪湿孤枕 提交于 2019-12-08 10:50:31

问题


I'm trying to share image on Instagram in my app. I've url of the image and using Picasso library to download image.

Target target = new Target() {
            @Override
            public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                Log.d(TAG, "Bitmap Loaded");
                File outputDir = getApplicationContext().getCacheDir(); // context being the Activity pointer
                try {
                    File outputFile = File.createTempFile("instagram", "png", outputDir);
                    outputFile.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(outputFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
                    ostream.close();
                    Log.d(TAG, "Image downloaded");
                    shareOnInstagram(Uri.fromFile(outputFile));
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        };
        Picasso.with(this).load(imageUrl).into(target);

But onBitmapLoaded is never being called. Is there any other way to share image on Instagram from a url? The intent which share on Instagram takes Intent.EXTRA_STREAM parameter which should be a media path on device. How do I convert an image from a url into that type?


回答1:


Picasso only keeps weak reference to target, so in your case it will be garbage collected. As a result, onBitmapLoaded is not being called.

You should store strong reference to target (make target member of your class).




回答2:


I using this approach

public class ShareToOtherApp extends AsyncTask<Bitmap, Void, Uri> {


    @Override
    protected Uri doInBackground(Bitmap... bitmaps) {

        return bitmaps.length > 0 ? BitmaptoUri(bitmaps[0]) : null;
    }

    @Override
    protected void onPostExecute(Uri uri) {


        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);

        if (uri != null) {
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        }

        shareIntent.setType("image/*");
        Intent chooserIntent = Intent.createChooser(shareIntent, "Share Image");
        chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MyApp.GetContext().startActivity(chooserIntent);

    }

    public File GetSDCardDir(){
        boolean ISSDCard;
        File[] Dirs = ContextCompat.getExternalFilesDirs(MyApp.GetContext(), null);

        ISSDCard = false;
        for (File Dir : Dirs) {
            if (Dir != null) {
                if (Dir.getPath().contains("sdcard")) {
                    ISSDCard = true;
                    break;
                }
            }
        }

        File SDCardDir;
        if(ISSDCard && Dirs[Dirs.length -1] != null){
            SDCardDir = Dirs[Dirs.length -1];
        }else{
            SDCardDir = Dirs[0];
        }

        return SDCardDir;
    }

    public Uri BitmaptoUri(Bitmap bitmap){
        Uri uri = null;
        try {
            File file =  new File(GetSDCardDir() , HConstants.IMG_FILE_NAME + ".jpg");

            if(file.getParentFile() != null){
                file.getParentFile().mkdirs();
            }else{
                GetSDCardDir().mkdirs();
            }
            file.createNewFile();

            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.close();

            uri = Uri.fromFile(file);
        } catch (IOException e) {

            e.printStackTrace();
        }

        return uri;
    }
}

and finally for using from it.

new ShareToOtherApp().execute(bitmap);


来源:https://stackoverflow.com/questions/33581706/android-load-image-with-picasso-onbitmaploaded-not-called

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!