2012-03-20 47 views
0

嗨我的應用程序中有一個畫廊,當您單擊縮略圖時,它會在圖像視圖中顯示圖像。我正在爲一個警告對話框設置一個長單擊偵聽器,該對話框有2個按鈕,一個用於設置壁紙,另一個用於分享。我有共享意圖和獲取圖形緩存有點工作。它適用於模擬器,但不適用於我的手機。我已經在這個網站上使用了很多例子來實現這個目標,但是它根本不工作。它保持關閉我的手機上的應用程序的力量。任何幫助表示讚賞。ImageView Share Intent

   alertDialog.setButton2("Share", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int which) { 
          imgView.setDrawingCacheEnabled(true); 
          Bitmap b = imgView.getDrawingCache(); 
          File sdCard = Environment.getExternalStorageDirectory(); 
          File file = new File(sdCard, "image.jpg"); 
          FileOutputStream fos; 
          try { 
           fos = new FileOutputStream(file); 
           b.compress(CompressFormat.JPEG, 95,fos); 
           } catch (FileNotFoundException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
           } 

          Log.d("Save Example", "Image saved."); 

          Intent intent = new Intent(Intent.ACTION_SEND); 
          intent.setType("image/jpeg"); 
          intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/image.jpg")); 

          startActivity(Intent.createChooser(intent, "Share Picture Via..")); 
         } 

      }); 

      alertDialog.show(); 
      return true; 

更新:它似乎是一個問題,

b.compress(CompressFormat.JPEG, 95, fos); 

口口聲聲說空指針。

原來,空指針實際上是一個read_only錯誤。所以它沒有實際寫入文件,我得到一個ioexception只讀文件系統。它爲什麼這樣做?

回答

0
imgView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imgView.layout(0, 0, imgView.getMeasuredWidth(), imgView.getMeasuredHeight()); 

看看之前

Bitmap b = imgView.getDrawingCache(); 

這有助於又見如果這有助於 Android drawing cache