2016-03-09 128 views
-2

我在我的應用程序中使用共享意圖,但我不能共享圖像和文本,我使用圖像和文本從我的JSON響應,但它不working.i我沒有得到任何錯誤,但是對於共享的方法是行不通的意圖共享不與圖像和文本共享

JSON響應:http://pastie.org/10753346

public void onShareItem(View v) { 
     // Get access to bitmap image from view 

     // Get access to the URI for the bitmap 
     Uri bmpUri = getLocalBitmapUri(descpic); 
     if (bmpUri != null) { 
      // Construct a ShareIntent with link to image 
      Intent shareIntent = new Intent(); 
      shareIntent.setAction(Intent.ACTION_SEND); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 
      shareIntent.putExtra(Intent.EXTRA_TEXT, desc.getText().toString()); 
      shareIntent.setType("image/*"); 
      // Launch sharing dialog for image 
      startActivity(Intent.createChooser(shareIntent, "Share Image")); 
     } else { 
      // ...sharing failed, handle error 
     } 
    } 

    // Returns the URI path to the Bitmap displayed in specified ImageView 
    public Uri getLocalBitmapUri(ImageView imageView) { 
    // Extract Bitmap from ImageView drawable 
    Drawable drawable = imageView.getDrawable(); 
    Bitmap bmp = null; 
    if (drawable instanceof BitmapDrawable){ 
     bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
    } else { 
     return null; 
    } 
    // Store image to default external storage directory 
    /*Uri bmpUri = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
     FileOutputStream out = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
     out.close(); 
     bmpUri = Uri.fromFile(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    }*/ 

    OutputStream fOut = null; 
    Uri outputFileUri=null; 
    try { 
     File root = new File(Environment.getExternalStorageDirectory() 
       + File.separator + "folder_name" + File.separator); 
     root.mkdirs(); 
     File sdImageMainDirectory = new File(root, "myPicName.jpg"); 
     outputFileUri = Uri.fromFile(sdImageMainDirectory); 
     fOut = new FileOutputStream(sdImageMainDirectory); 
    } catch (Exception e) { 
     Toast.makeText(this, "Error occured. Please try again later.", 
       Toast.LENGTH_SHORT).show(); 
    } 

    try { 
     bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
     fOut.flush(); 
     fOut.close(); 
    } catch (Exception e) { 
    } 
    return outputFileUri; 
} 
+0

看起來像'bmpUri'爲空。嘗試在'onShareItem'的'else'部分添加一個日誌語句。 – Rohit5k2

+0

@ Rohit5k2你是對的我的bmpuri是空的..我把其中一個日誌放在logcat中。 – albert

+0

爲什麼它變爲空? – albert

回答

0

這樣做是爲了保存圖像。用此代替您的方法

public Uri getLocalBitmapUri(ImageView imageView) { 
    imageview.buildDrawingCache(); 
    Bitmap bm = imageview.getDrawingCache(); 

    OutputStream fOut = null; 
    Uri outputFileUri; 
    try { 
     File root = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "folder_name" + File.separator); 
     root.mkdirs(); 
     File imageFile = new File(root, "myPicName.jpg"); 
     outputFileUri = Uri.fromFile(imageFile); 
     fOut = new FileOutputStream(imageFile); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try { 
     bm.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
     fOut.flush(); 
     fOut.close(); 
     return outputFileUri; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
+0

我需要你的幫助 – albert