通过意图发送图像

时间:2014-11-18 09:49:58

标签: android gridview

我编写了一个程序,其中我的图像从SD卡中读取并以gridview显示,当点击gridview上的任何图像时,更大的图像在另一个活动中打开,我还添加了一个共享按钮以共享我的图像到不同的应用程序示例whatsapp,所以当我点击共享时,一个选择器弹出窗口,我也安全地到达whatsapp里面,我选择一个用户但是当我选择一个用户我的图像没有显示时,下面是我选择一个后屏幕的图片用户

enter image description here

我上面的代码

public class FullScreenImageAdapter extends PagerAdapter {

private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
Intent intent, chooser;

// constructor
public FullScreenImageAdapter(Activity activity,
        ArrayList<String> imagePaths) {
    this._activity = activity;
    this._imagePaths = imagePaths;
}

@Override
public int getCount() {
    return this._imagePaths.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(final ViewGroup container, int position) {
    TouchImageView imgDisplay;
    final Button btnClose, btnShare;

    inflater = (LayoutInflater) _activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_imag,
            container, false);

    imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
    btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
    btnShare = (Button) viewLayout.findViewById(R.id.btnShare);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position),
            options);
    imgDisplay.setImageBitmap(bitmap);


    btnShare.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            intent = new Intent(Intent.ACTION_SEND);
            Uri imageuri = Uri.parse("file://sdcard//Pictures//raw "
                    + _imagePaths);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_STREAM, imageuri);
            chooser = Intent.createChooser(intent, "Share image via....");
            container.getContext().startActivity(chooser);

        }
    });

    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}

我需要知道我哪里出错了,请提出建议 感谢你

3 个答案:

答案 0 :(得分:0)

Bitmap bm = BitmapFactory.decodeResource(getResources(), image);
    File f = new File(getExternalCacheDir() + "/image.png");
    try {
        FileOutputStream outStream = new FileOutputStream(f);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_SEND);
    intent.setDataAndType(Uri.fromFile(f), "image/*");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
    intent.putExtra(Intent.EXTRA_TEXT, "Share From : XYZ");
    startActivity(Intent.createChooser(intent, "Share image using"));

答案 1 :(得分:0)

似乎在你的Image Uri创作中有一个“白色空间”:

Uri imageuri = Uri.parse("file://sdcard//Pictures//raw "
                + _imagePaths);

“原始”之后的空格

答案 2 :(得分:0)

试试这个 -

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    Uri uri = Uri.parse(YourImage's uri here);
                whatsappIntent.setType("image/jpg"); 
                whatsappIntent.putExtra(Intent.EXTRA_STREAM, uri); 
    whatsappIntent.setPackage("com.whatsapp");

    try {
        activity.startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        ToastHelper.MakeShortText("Whatsapp have not been installed.");
    }