-1

我在我的应用程序中使用共享意图,但我无法共享图像和文本,我正在使用我的 json 响应中的图像和文本,但它不起作用。我没有收到任何错误,但是共享方法不管用

JSON 响应: 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;
}
4

1 回答 1

0

这样做可以保存图像。用这个替换你的方法

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;
}
于 2016-03-09T11:43:51.823 回答