0

我正在從XML資源中讀取矢量圖像並在ImageView中顯示它。沒關係。 現在,我需要通過intent.putExtra將圖像傳遞給一個Intent。 問題:如何將矢量XML轉換爲位圖或從ImageView獲取圖像? 我試過.getDrawingCache(),.getDrawable,getImageMatrix等,但它不起作用。如何從XML資源中的矢量圖像檢索位圖

試過這樣:

String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), 
      imgPrevisao.getDrawingCache(), 
      "Minha previsão",null); 

    Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
    sharingIntent.setType("image/*"); 
    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path)) ; 
    getApplicationContext().startActivity(Intent.createChooser(sharingIntent, "Share with")); 

而且這樣:

Intent intent = new Intent(Intent.ACTION_SEND); 

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    intent.putExtra(Intent.EXTRA_TEXT, textoPrevisao); 
    intent.putExtra(Intent.EXTRA_STREAM, imgPrevisao.getDrawingCache()); 
    intent.setType("image/*"); 
    startActivity(intent); 

TIA,

安德烈·科雷亞

+0

類似的東西應該工作。漂亮的標準操作來從圖像視圖中獲取位圖。 – greenapps

+0

這個意圖應該在你的應用程序中處理嗎? – j2ko

+0

greenapps,對不起,像什麼?我是Android開發新手。 –

回答

0

的方法之一是使用MediaStore

public static Uri getImageUri(Context context, Bitmap bmp) { 
    String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), , "share_title",bmp ,null); 
    return Uri.parse(path); 
} 

比:

public static void shareImageViewContent(ImageView view) { 
    Bitmap bitmap = ((BitmapDrawable)view.getDrawable()).getBitmap(); 
    Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
    sharingIntent.setType("image/jpeg"); 
    sharingIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(bitmap)) ; 
    context.startActivity(Intent.createChooser(sharingIntent, "Share with")); 
} 

一個比較複雜的解決方案是使用FileProvider 學分:linklinklink

+0

謝謝j2ko,我會試試看。我沒有足夠的聲望來支持你的答案,對不起。 –

+0

@AndréAranhanp,我很樂意提供幫助。但我想你可以將我的解決方案標記爲回答 – j2ko

+0

它不起作用。我仍在嘗試。我會發布我嘗試過的。 –