2014-02-05 57 views
1

即时尝试更改“Intent.EXTRA_STREAM”文件名。 即时通讯使用图像加载器加载图像,它会缓存本地SD卡上的文件。 现在我试图将这些图像附加到电子邮件意图并发送它们。 问题是文件的名称包含没有扩展名,这个问题在其他共享mehtod像bluetoosh或一些电子邮件hayoola。 Imageloader获取URL并将它们存储在SD卡上。 有上ImageLoader的一个函数,我通过URL并返回URI:即时更改Intent.EXTRA_STREAM名称

public Uri getUri(String url) 
{ 
    File f=fileCache.getFile(url); 
    return Uri.fromFile(f); 
} 

这里是我的代码:

emailIntent.putExtra(Intent.EXTRA_STREAM, (imageloader.getUri(ImgUrl))); 

现在的问题是,这是没有扩展名的文件的名称返回:

file:///mnt/sdcard/mygallery/896105659 

我尝试添加 “+” 巴纽”“,但它返回错误显示java.lang.NullPointerException

的网址:它是一个正常的图像URL: “http://www.ischgl.com/website/var/tmp/image-thumbnails/41395/thumb__lightbox/vider-alp_81712.jpeg” IM使用这FileCache:

String filename=String.valueOf(url.hashCode()); 
+0

你能告诉我们你的一个原始网址吗? –

回答

3

好,我找到了一个解决方法连接前更改名称:

File fileOutput=null; 
     try { 
      fileOutput = new File(Environment.getExternalStorageDirectory() + File.separator + "MyImage_.png"); 
      fileOutput.createNewFile(); 
      copyFile(Imageloader.getFile(ImageUrl), fileOutput); 
     } catch (IOException e) { 
        // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileOutput)); 
startActivity(Intent.createChooser(emailIntent, getResources().getString(R.string.share))); 

和复制:

void copyFile(File src, File dst) throws IOException { 
    FileChannel inChannel = new FileInputStream(src).getChannel(); 
    FileChannel outChannel = new FileOutputStream(dst).getChannel(); 
    try { 
     inChannel.transferTo(0, inChannel.size(), outChannel); 
    } finally { 
     if (inChannel != null) 
      inChannel.close(); 
     if (outChannel != null) 
      outChannel.close(); 
    } 
}