意向共享与图片和文字交流工作 [英] Intent share not working with image and text sharing

查看:245
本文介绍了意向共享与图片和文字交流工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用份额的意图在我的应用程序,但我不能够分享图片和文字,我使用的图像和文字从我的JSON响应,但它不是我working.i没有得到任何错误,但是在共享的方法是行不通的。

JSON响应: http://pastie.org/10753346

 公共无效onShareItem(视图v){
        //从视图访问位图图像        //获取访问的URI为位图
        乌里bmpUri = getLocalBitmapUri(descpic);
        如果(bmpUri!= NULL){
            //构造一个与ShareIntent链接图像
            意图shareIntent =新的Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM,bmpUri);
            shareIntent.putExtra(Intent.EXTRA_TEXT,desc.getText()的toString());
            shareIntent.setType(图像/ *);
            //启动共享对话框图像
            startActivity(Intent.createChooser(shareIntent,分享图片));
        }其他{
            // ...共享失败,处理错误
        }
    }    //返回按指定的ImageView中显示的位图的URI路径
    公共乌里getLocalBitmapUri(ImageView的ImageView的){
    //从ImageView的绘制提取位图
    可绘制可绘制= imageView.getDrawable();
    BMP位图= NULL;
    如果(绘制的instanceof BitmapDrawable){
        BMP =((BitmapDrawable)imageView.getDrawable())getBitmap();
    }其他{
        返回null;
    }
    //专卖店形象到默认外部存储目录
    / *乌里bmpUri = NULL;
    尝试{
        档案文件=新的文件(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS),share_image_+ System.currentTimeMillis的()+.PNG);
        file.getParentFile()mkdirs()。
        FileOutputStream中出=新的FileOutputStream(文件);
        bmp.com preSS(Bitmap.Com pressFormat.PNG,90出);
        out.close();
        bmpUri = Uri.fromFile(文件);
    }赶上(IOException异常五){
        e.printStackTrace();
    } * /    OutputStream的FOUT = NULL;
    乌里outputFileUri = NULL;
    尝试{
        文件根=新的文件(Environment.getExternalStorageDirectory()
                +文件分割符+文件夹名+文件分割符);
        root.mkdirs();
        文件sdImageMainDirectory =新的文件(根,myPicName.jpg);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
        FOUT =新的FileOutputStream(sdImageMainDirectory);
    }赶上(例外五){
        Toast.makeText(这一点,发生错误,请稍后再试。
                Toast.LENGTH_SHORT).show();
    }    尝试{
        bmp.com preSS(Bitmap.Com pressFormat.PNG,100,FOUT);
        fOut.flush();
        fOut.close();
    }赶上(例外五){
    }
    返回outputFileUri;
}


解决方案

这样做是为了保存形象。这种替换你的方法

 公众开放的getLocalBitmapUri(ImageView的ImageView的){
    imageview.buildDrawingCache();
    位图BM = imageview.getDrawingCache();    OutputStream的FOUT = NULL;
    乌里outputFileUri;
    尝试{
        文件根=新的文件(Environment.getExternalStorageDirectory()
            +文件分割符+文件夹名+文件分割符);
        root.mkdirs();
        文件镜像文件=新的文件(根,myPicName.jpg);
        outputFileUri = Uri.fromFile(镜像文件);
        FOUT =新的FileOutputStream(镜像文件);
    }赶上(例外五){
        e.printStackTrace();
    }    尝试{
        bm.com preSS(Bitmap.Com pressFormat.PNG,100,FOUT);
        fOut.flush();
        fOut.close();
        返回outputFileUri;
    }赶上(例外五){
        e.printStackTrace();
    }
    返回null;
}

I am using share intent in my application,but i am not able to share image and text,i am using image and text from my json response,but it is not working.i am not getting any error,but the the method for sharing is not working

JSON Response : http://pastie.org/10753346

public void onShareItem(View v) {
        // Get access to bitmap image from view

        // Get access to the URI for the bitmap
        Uri bmpUri = getLocalBitmapUri(descpic);
        if (bmpUri != null) {
            // Construct a ShareIntent with link to image
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
            shareIntent.putExtra(Intent.EXTRA_TEXT, desc.getText().toString());
            shareIntent.setType("image/*");
            // Launch sharing dialog for image
            startActivity(Intent.createChooser(shareIntent, "Share Image"));
        } else {
            // ...sharing failed, handle error
        }
    }

    // Returns the URI path to the Bitmap displayed in specified ImageView
    public Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable){
        bmp = ((BitmapDrawable) imageView.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() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }*/

    OutputStream fOut = null;
    Uri outputFileUri=null;
    try {
        File root = new File(Environment.getExternalStorageDirectory()
                + File.separator + "folder_name" + File.separator);
        root.mkdirs();
        File sdImageMainDirectory = new File(root, "myPicName.jpg");
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
        fOut = new FileOutputStream(sdImageMainDirectory);
    } catch (Exception e) {
        Toast.makeText(this, "Error occured. Please try again later.",
                Toast.LENGTH_SHORT).show();
    }

    try {
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
    }
    return outputFileUri;
}

解决方案

Do this to save image. Replace your method with this

public Uri getLocalBitmapUri(ImageView imageView) {
    imageview.buildDrawingCache();
    Bitmap bm = imageview.getDrawingCache();

    OutputStream fOut = null;
    Uri outputFileUri;
    try {
        File root = new File(Environment.getExternalStorageDirectory()
            + File.separator + "folder_name" + File.separator);
        root.mkdirs();
        File imageFile = new File(root, "myPicName.jpg");
        outputFileUri = Uri.fromFile(imageFile);
        fOut = new FileOutputStream(imageFile);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        return outputFileUri;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

这篇关于意向共享与图片和文字交流工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆