2012-08-05 38 views
2

我看到关于此问题的其他问题,尝试了他们的答案,但不工作。 我试图使用developer.android.com的第二种方法。 (第二个白色子弹)。 我用适当的权限将图像保存到应用程序内部存储器。 图像(位图)已成功保存,但我无法将其添加到新的意图中,以便共享。 这里是我的代码:无法附加位图图像。 Android

 Intent shareIntent = new Intent(Intent.ACTION_SEND); 
     shareIntent.setType("*/*");//Attach all types (images/text) 

     FileOutputStream fos; 

     try{ 
     fos = openFileOutput(myfilename, Context.MODE_WORLD_READABLE); 
     mybitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); 
     fos.flush(); 
//Image is stored successfully (checked data/data/mypackage/files/myfilename) 

      Uri uri = Uri.fromFile(getFileStreamPath(myfilename)); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
    } 
    catch (Exception e){ 
     // noth 
     //Log.e(TAG, e.getStackTrace().toString()); 
    } 

     shareIntent.putExtra(Intent.EXTRA_TEXT, "some text also"); 

     return shareIntent; 
    } 

应用程序说,不能附加媒体项目。 Gmail似乎附加了项目,但邮件发送时什么都不显示!

另外,uri.getPath返回我: /数据/数据/ mypackage的/文件/ mYfILEname的, 这是正确的

任何想法? 谢谢!

编辑: 使用SD卡的修改代码。 ,但仍没有得到它的工作:

 File imgDir; 
     if (android.os.Environment.getExternalStorageState().equals(
       android.os.Environment.MEDIA_MOUNTED)) 
imgDir = new File(
       android.os.Environment.getExternalStorageDirectory(), 
       ".MyAppName/Images"); 
     else imgDir = MyActivity.this.getCacheDir(); 
     if (!imgDir.exists()) imgDir.mkdirs(); 

     File output = new File(imgDir, myFilename); 

     OutputStream imageFileOS; 
     try{ 
      Uri uriSavedImage = Uri.fromFile(output); 
      imageFileOS = getContentResolver().openOutputStream(
        uriSavedImage); 
      bitmapBookCover.compress(Bitmap.CompressFormat.PNG, 90, 
        imageFileOS); 


      imageFileOS.flush(); 
      imageFileOS.close(); 
//Again image successfully saved 

      shareIntent.putExtra(Intent.EXTRA_STREAM, uriSavedImage); 
     } 
     catch (Exception e){ 
      // noth 
     } 
+0

我认为这是不可能的从试过内部存储 – eladr 2012-08-05 07:01:25

+0

武官文件与SD卡,仍然不能得到它的工作 – Paschalis 2012-08-05 07:29:39

+0

@Paschalis你成功了吗? – Huaying 2016-11-04 09:55:02

回答

0

请尝试usiing 使用setData()的代替putExtra(),这里android developer site - intent set data

+0

我附上文字和图片。它不起作用。尝试shareIntent.setDataAndType(uri,Intent.EXTRA_STREAM),图像和文本:shareIntent.setDataAndType(bookInfo,Intent.EXTRA_TEXT),没有运气 – Paschalis 2012-08-05 07:47:49