2016-02-13 49 views
0

我正在尝试分享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(); 

} 
+0

你会得到什么问题?碰撞?如果是这样,请发布LogCat – user2340612

+0

@ user2340612当我要使用社交应用程序发送文件时,我没有发生崩溃它只是出现如下消息:**共享失败,请再试一次**(在Whatsapp的例子)或**不可能上传图片**(在Instagram的情况下) – Pier

+0

你确定你需要'EXTRA_TEXT'和'EXTRA_STREAM'临时演员吗?我想你只需要第二个 – user2340612

回答

0

添加的权限从文件名(图像)您的清单

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

File file = new File(mFolder.getAbsolutePath(),filename +“.jpg”);

您错过了两个参数中的一个/两个参数。

Wonderful blogpost about storing image

+0

我试着把** return file.getAbsolutePath()**但没有任何东西 – Pier

+0

你得到了什么?图像保存到存储? – SreeAndroidDev

+0

检查编辑以查看我现在得到的内容 – Pier