【问题标题】:Sharing image failed分享图片失败
【发布时间】:2016-02-13 23:03:52
【问题描述】:

我正在尝试分享image,但我不知道为什么我失败了,请你帮帮我吗?

String imageUrl = web.get(position).getImage();
    if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))
        imageUrl = "http://" + imageUrl;

    Button button = (Button)rowView.findViewById(R.id.condividi);
    final String finalImageUrl = imageUrl;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_TEXT, web.get(position).getTitle());
            File file = writebitmaptofilefirst("the image", finalImageUrl );
            Uri path = Uri.fromFile(file);
            intent.putExtra(Intent.EXTRA_STREAM, path );
            Intent send = Intent.createChooser(intent, null);
            context.startActivity(send);
        }
    });

public static File writebitmaptofilefirst(String filename, String source) {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File mFolder = new File(extStorageDirectory + "/temp_images");
    if (!mFolder.exists()) {
        mFolder.mkdir();
    }
    OutputStream outStream = null;


    File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
    if (file.exists()) {
        file.delete();
        file = new File(extStorageDirectory, filename + ".jpg");
        Log.e("file exist", "" + file + ",Bitmap= " + filename);
    }
    try {
        URL url = new URL(source);
        Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("file", "" + file);
    return file;

}

编辑

String imageUrl = web.get(position).getImage();
    if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))
        imageUrl = "http://" + imageUrl;

    Button button = (Button)rowView.findViewById(R.id.condividi);
    final String finalImageUrl = imageUrl;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_TEXT, web.get(position).getTitle());
            String file = writebitmaptofilefirst("ndp_image", finalImageUrl);
            //Uri path = Uri.fromFile(file);
            intent.putExtra(Intent.EXTRA_STREAM, file );
            Intent send = Intent.createChooser(intent, null);
            context.startActivity(send);
        }
    });

    return rowView;

}

public static String writebitmaptofilefirst(String filename, String source) {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File mFolder = new File(extStorageDirectory + "/temp_images/");
    if (!mFolder.exists()) {
        mFolder.mkdir();
    }
    OutputStream outStream = null;


    File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
    if (file.exists()) {
        file.delete();
        file = new File(extStorageDirectory, filename + ".jpg");
        Log.e("file exist", "" + file + ",Bitmap= " + filename);
    }
    try {
        URL url = new URL(source);
        Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("file", "" + file);
    return file.getAbsolutePath();

}

【问题讨论】:

  • 你遇到了什么问题?崩溃?如果是这样,请也发布 LogCat
  • @user2340612 我没有崩溃,当我准备使用社交应用发送文件时它只是显示如下消息:共享失败,请重试 (在 Whatsapp 的情况下)或 无法上传图片(在 Instagram 的情况下)
  • 您确定同时需要EXTRA_TEXTEXTRA_STREAM 附加功能吗?我猜你只需要第二个
  • 但我需要与图片分享一些文字
  • 也许this SO answer可以帮到你

标签: android image android-intent sharing


【解决方案1】:

向您的清单添加权限

从文件名(图像)中删除空格。有了空间,您需要解码 uri。您应该传递完整路径return file.getAbsolutePath()。您只是在传递文件名。
在文件存在的情况下,您不会存储在同一路径中。你没有被包括在字典里。 extranlstoragepath+/temp_images/+the image.jpg 尝试记录您的文件路径。和

文件 file = new File(mFolder.getAbsolutePath(), filename + ".jpg");

您错过了两个参数之间的 /。

Wonderful blogpost about storing image

【讨论】:

  • 我试过放 return file.getAbsolutePath() 但什么都没有
  • 你得到了什么?图像保存到存储?
  • 你添加权限了吗?
  • 我不敢相信:我添加了 而不是 我之前的代码很好
猜你喜欢
  • 2017-07-01
  • 1970-01-01
  • 2015-04-05
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 2017-10-24
  • 2016-12-23
相关资源
最近更新 更多