2017-04-13 60 views
0

夥計們,我一直面臨着共享牛軋糖設備的問題,但不知道爲什麼選擇共享應用程序(ex-facebook)後,我的圖像是不可見。這裏是低於只有在牛軋糖設備共享意圖後才能看到圖像

enter image description here

share.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
        Locale.getDefault()).format(new Date()); 
      Bitmap bitmap = viewToBitmap(canvas); 
      shareBitmap(bitmap, timeStamp + ".png"); 
     } 
    }); 


    private void shareBitmap(Bitmap bitmap, String fileName) { 
    try { 

     File file = new File(getCacheDir(), fileName + ".png"); 
     FileOutputStream fOut = new FileOutputStream(file); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
     fOut.flush(); 
     fOut.close(); 
     file.setReadable(true, false); 
     final Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
     intent.setType("image/png"); 
     startActivity(Intent.createChooser(intent, "Share Image")); 
    } catch (Exception e) { 
     Log.e("exception :>>", e.toString()); 
     e.printStackTrace(); 
    } 
} 

截圖使用FileProvider:

AndroidManifest.xml中

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="${applicationId}.provider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/provider_paths"/> 
    </provider> 

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <external-path name="external_files" path="."/> 
</paths> 

save_share.java

share.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
        Locale.getDefault()).format(new Date()); 
      Bitmap bitmap = viewToBitmap(canvas); 
      shareBitmap(bitmap, timeStamp + ".png"); 
     } 
    }); 


    private void shareBitmap(Bitmap bitmap, String fileName) { 
    try { 

     File file = new File(getCacheDir(), fileName + ".png"); 
     FileOutputStream fOut = new FileOutputStream(file); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
     fOut.flush(); 
     fOut.close(); 
     file.setReadable(true, false); 
    Uri photoURI = FileProvider.getUriForFile(save_share.this, BuildConfig.APPLICATION_ID + ".provider", file); 
     final Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     intent.putExtra(Intent.EXTRA_STREAM, photoURI); 
     intent.setType("image/png"); 
     startActivity(Intent.createChooser(intent, "Share Image")); 
    } catch (Exception e) { 
     Log.e("exception :>>", e.toString()); 
     e.printStackTrace(); 
    } 
} 

這僅與牛軋糖設備發生。請幫我找到一個解決方案

回答

1

這隻發生在牛軋糖設備上。

您已將圖像存儲在應用的內部存儲器部分,而其他應用無法訪問該圖像。

您試圖通過呼叫setReadable(true, false)來解決此問題。這是不安全的,因爲它允許任何應用程序讀取此文件。而且,從Android 7.0開始,IIRC,setReadable()不能再使用。使用FileProvider與其他應用程序共享內容。

+0

我試圖FileProvider也不過能不能打開共享對話框有了它,看到我更新了我的代碼 – Sudhansu

+1

@Sudhansu:好吧,除了其他可能的問題,您的'provider_paths.xml'是錯誤的。它說你的文件在外部存儲器上,而不是。使用'',因爲[FileProvider'文檔](https://developer.android.com/reference/android/support/v4/content/FileProvider.html#SpecifyFiles)顯示,映射到'getCacheDir() '你正在使用。 – CommonsWare

+0

@Sudhansu:'name'是你想要的任何東西,這是一個有效的'Uri'路徑段。你的情況不需要'路徑'。 – CommonsWare