2012-08-12 163 views
2

我有一个Activity用户可以从raw文件夹共享图像。Android:从原始文件夹共享图像。错误的图像共享

原始文件夹有70个图像,全部按字母顺序排列。第一个是R.raw.recipe01,最后一个是R.raw.recipe70

我得到的图像int我想从Bundle分享,我有一个方法将图像从raw文件夹复制到可访问的文件。

我打电话startActivity(createShareIntent());ActionBarMenuItem,它工作成功。

问题

份额intent将始终选择R.raw.recipe01的图像,即使从Bundleint是图像〔实施例R.raw.recipe33

我已在下面分享我的代码。任何人都可以发现我做错了什么吗?

CODE:

private int rawphoto = 0; 
private static final String SHARED_FILE_NAME = "shared.png"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    Bundle bundle = getIntent().getExtras(); 
    rawphoto = bundle.getInt("rawphoto"); 
    int savedphoto = rawphoto; 

    // COPY IMAGE FROM RAW 
    copyPrivateRawResourceToPubliclyAccessibleFile(savedphoto); 


private Intent createShareIntent() { 
    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.setType("image/*"); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, "IMAGE TO SHARE: "); 
    Uri uri = Uri.fromFile(getFileStreamPath("shared.png")); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 

    return shareIntent; 
} 


private void copyPrivateRawResourceToPubliclyAccessibleFile(int photo) { 

    System.out.println("INT PHOTO: " +photo); 

    InputStream inputStream = null; 
    FileOutputStream outputStream = null; 
    try { 
     inputStream = getResources().openRawResource(photo); 
     outputStream = openFileOutput(SHARED_FILE_NAME, 
       Context.MODE_WORLD_READABLE | Context.MODE_APPEND); 
     byte[] buffer = new byte[1024]; 
     int length = 0; 
     try { 
      while ((length = inputStream.read(buffer)) > 0) { 
       outputStream.write(buffer, 0, length); 
      } 
     } catch (IOException ioe) { 
      /* ignore */ 
     } 
    } catch (FileNotFoundException fnfe) { 
     /* ignore */ 
    } 

    finally { 
     try { 
      inputStream.close(); 
     } catch (IOException ioe) { 

     } 
     try { 
      outputStream.close(); 
     } catch (IOException ioe) { 

     } 
    } 

} 
+1

为什么Context.MODE_APPEND? – 2012-08-12 11:39:59

+0

@JoelSjögren我不知道这是为什么,但我已经删除它,它现在起作用。谢谢!将您的评论移至答案,以便我可以接受:-) – tiptopjat 2012-08-12 11:46:11

+0

此问题是我的答案。如何从原始文件夹共享图像。良好的祝福! – 2014-07-28 06:10:38

回答

1

删除Context.MODE_APPEND这样,如果它已经存在的文件被覆盖。