点击"分享图像"没有从毕加索获得图像uri。按键

时间:2017-10-24 20:38:45

标签: android

我使用Picasso库在PhotoView中加载图像,但是在使用getLocalBitmapUri时我无法获得imageUri。

package com.shahryarsoftwares.chatbook;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.github.chrisbanes.photoview.PhotoView;
import com.github.chrisbanes.photoview.PhotoViewAttacher;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageViewerActivity extends AppCompatActivity {
private Toolbar mToolbar;
private PhotoView photoView;
private PhotoViewAttacher mAttacher;
private Button shareBtn;

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

    Bundle extras = getIntent().getExtras();
    String user_name = extras.getString("user_name");
    final String user_image = extras.getString("user_image");

    mToolbar = (Toolbar) findViewById(R.id.image_viewer_toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle(user_name);

    photoView = (PhotoView) findViewById(R.id.photo_view);
    Picasso.with(ImageViewerActivity.this).load(user_image).placeholder(R.drawable.default_profile).into(photoView);

    mAttacher = new PhotoViewAttacher(photoView);

    shareBtn = (Button) findViewById(R.id.image_viewer_share_btn);
    shareBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                    Uri bmpUri = getLocalBitmapUri(photoView);
                    if(bmpUri != null){
                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                        shareIntent.setType("image/*");
                        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        startActivity(Intent.createChooser(shareIntent, "Share Image"));
                    }else{
                        Toast.makeText(ImageViewerActivity.this, "No image selected", Toast.LENGTH_SHORT).show();
                    }
        }
    });



    //mAttacher.update();
}
public Uri getLocalBitmapUri(PhotoView photoView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = photoView.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable){
        bmp = ((BitmapDrawable) photoView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file =  new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".jpg");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}
}

我在哪里弄错了?我从另一个活动中获取输入图像URI并使用putExtra()并使用getExtra()获取该活动。

我还使用了以下代码:

   shareBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Target shareTarget = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

                    Uri bmpUri = getLocalBitmapUri(photoView);
                    if(bmpUri != null){
                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                        shareIntent.setType("image/*");
                        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        startActivity(Intent.createChooser(shareIntent, "Share Image"));
                    }else{
                        Toast.makeText(ImageViewerActivity.this, "No image selected", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            };
            Picasso.with(ImageViewerActivity.this).load(user_image).placeholder(R.drawable.default_profile).into(shareTarget);
            startShare(user_image);
        }
    });

但也是这样,它没有得到解决。

0 个答案:

没有答案