2016-12-16 2 views
-2

私の古いコードはマシュマロで働いていなかったので、このシェアコードを書いた。私はそれをはっきりと混乱させ、結果は得られませんでした。 URLから画像を共有する他の簡単な方法がある場合は、教えてください。共有オプションがマシュマロで働いていない

public void share(View view){ 
    niv1 = (NetworkImageView) findViewById(R.id.imgNetwork); 
    File file = getLocalBitmapFile(niv1); 
    Uri bmpUrii = FileProvider.getUriForFile(this, "com.myfileprovider", file); 
    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.setType("image/*"); 
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUrii); 
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "share"); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, Datas.Address); 
    startActivity(Intent.createChooser(shareIntent, "Share Image")); 
     } 

public File getLocalBitmapFile(NetworkImageView imageView) { 

    Drawable drawable = imageView.getDrawable(); 
    Bitmap bmp = null; 
    if (drawable instanceof BitmapDrawable) { 
     bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
    } else { 
     return null; 
    } 
    File bmpUri = null; 

     File file = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
    FileOutputStream out = null; 
    try { 
     out = new FileOutputStream(file); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
    try { 
     out.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return file; 
} 

答えて

1
Uri fileUri = FileProvider.getUriForFile(this, "com.myfileprovider", new File(String.valueOf(niv1))) 

niv1NetworkImageViewです。ファイルパスではなく、ファイルパスを指していません。

XMLメタデータに基づいてFileProviderによって管理されている場所のファイルを指しているFileオブジェクトをnew File(String.valueOf(niv1))に置き換えます。 This sample appFileProviderを使用して説明していますが、この場合はPDF用であり、ACTION_VIEWです。

+0

私はそれを少し変更しました。助けてください – Sreepulari

+0

私は問題のコードを変更しました、私はそれを編集する必要がある教えてください。 – Sreepulari

+0

@Sreepulari:ビットマップを 'DIRECTORY_DOWNLOADS'に書き出しています。これは、ファイルが 'FileProvider'であるとは言えません。そこに' DIRECTORY_PICTURES'を使っています。 'getLocalBitmapUri()'を 'getLocalBitmapFile()'に名前を変更します。ビットマップを保存している 'File'オブジェクトを返します。同じ 'File'オブジェクトを' getUriForFile() 'と一緒に使用してください。 – CommonsWare

関連する問題