在棉花糖中分享来自网址的图像

时间:2016-12-18 06:14:59

标签: android

我的旧代码只能在lolipop上运行良好。我试着写一个新的,但它也没有在棉花糖中工作。最近两天我一直坐在这里,我没有线索:(。

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;
}

0 个答案:

没有答案