2017-03-09 115 views
-1

舊的共享代碼不再在API 23上面工作。我從url加載圖像在TouchImageView中,我想分享圖片到WhatsApp,Facebook等我找不到在互聯網的任何可靠的新代碼和我想一些方法來分享,但他們都failed.Here是我的代碼分享圖像從URL到Facebook,WhatsApp

public class GalleryActivityshare extends DialogFragment 
{ 

TouchImageView iv; 
String url; 

static GalleryActivityshare newInstance() { 
    GalleryActivityshare f = new GalleryActivityshare(); 
    return f; 
} 

@Override 
public void onStart() 
{ 
    super.onStart(); 
    Dialog dialog = getDialog(); 
    if (dialog != null) 
    { 
     int width = ViewGroup.LayoutParams.MATCH_PARENT; 
     int height = ViewGroup.LayoutParams.MATCH_PARENT; 
     dialog.getWindow().setLayout(width, height); 
    } 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.activity_galleryshare, container, false); 


    url=getArguments().getString("url"); 

    iv = (TouchImageView) v.findViewById(R.id.imgDisplay); 

    new ImageLoadTask(url,iv).execute(); 

    return v; 

} 
public void share(Context context, Uri bmpUri) throws IOException { 
     Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.id.imgDisplay); 
     File file = new File(getActivity().getCacheDir(), String.valueOf(largeIcon 
       + ".png")); 
     FileOutputStream fOut = new FileOutputStream(file); 
     largeIcon.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
     fOut.flush(); 
     fOut.close(); 
     file.setReadable(true, false); 
     final Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
     intent.setType("image/png"); 
     startActivity(intent); 

    } 


} 

XML

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#ffffff"> 

<com.touchlive.info.paramount.util.TouchImageView 

    android:id="@+id/imgDisplay" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:scaleType="fitCenter" /> 

<ImageView 
    android:tint="#123654" 
    android:onClick="share" 
    android:id="@+id/shared" 
    android:src="@drawable/share" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_width="40dp" 
    android:layout_height="40dp" /> 

+0

使用logcat的研究與應用相關聯的Java堆棧跟蹤:https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

我更改了代碼,請看 – almiters

回答

2

嘗試{

   Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), Your-Image); 
       File file = new File(PageView.this.getCacheDir(), String.valueOf(largeIcon 
         + ".png")); 
       FileOutputStream fOut = new FileOutputStream(file); 
       largeIcon.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
       fOut.flush(); 
       fOut.close(); 
       file.setReadable(true, false); 
       final Intent intent = new Intent( Intent.ACTION_SEND); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
       intent.setType("image/png"); 
       startActivity(intent); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
+0

您好我已經編輯了我的代碼中的onClick共享。我已經把iv而不是'pageview.this'它顯示錯誤消息:( – almiters

+0

它顯示錯誤? –

+0

它顯示零點異常 – almiters