簡體   English   中英

如何從XML資源中的矢量圖像中檢索位圖

[英]How to retrieve a bitmap from a vector image in a XML resource

我正在從XML資源讀取矢量圖像,並將其顯示在ImageView中。 沒關系。 現在,我需要使用intent.putExtra將圖像傳遞給Intent。 問題:如何將矢量XML轉換為位圖或從ImageView獲取圖像? 我嘗試了.getDrawingCache()、. getDrawable,getImageMatrix等,但是它不起作用。

嘗試過這種方式:

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,

安德烈·科雷阿(AndréCorrêa)

一種方法是使用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

我知道那是很久以前的事了,但是如果有人最終有同樣的疑問,我希望它能有所幫助。 我想共享圖像和文本,因為我無法按照自己的方式進行操作,因此制作了屏幕截圖並進行了共享。

private void shareScreenShot() {

try {
    // image naming and path  to include sd card  appending name you choose for file
    String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                   .toString()+"/Zodiaco";

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    //creates the directory
    File pasta = new File(mPath);
    if (!pasta.exists()) {
        pasta.mkdir();
    }

    //creates the image
    File imageFile = new File(mPath+ "/" + getDate() + ".jpg");
    FileOutputStream outputStream = new FileOutputStream(imageFile);
    if(!imageFile.exists()){
        imageFile.createNewFile();
    }
    int quality = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
    outputStream.flush();
    outputStream.close();

    Uri uri = Uri.fromFile(imageFile);

    // creates the intent and defines its action
    Intent intent = new Intent( Intent.ACTION_SEND );
    intent.setDataAndType(uri, "image/*");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    // inicia a intent
    startActivity( intent );

} catch (Throwable e) {

    Toast.makeText(this, "Não foi possível compartilhar a previsão.",
                   Toast.LENGTH_LONG).show();

    e.printStackTrace();
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM