共享图像失败

时间:2016-02-13 23:03:53

标签: android image android-intent sharing

我试图分享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();

}

1 个答案:

答案 0 :(得分:0)

为清单添加权限

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

文件文件=新文件(mFolder.getAbsolutePath(),文件名+" .jpg");

你错过了两个参赛者之间的比赛。

Wonderful blogpost about storing image