简体   繁体   English

如何从XML资源中的矢量图像中检索位图

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

I am reading a vector image from a XML resource and showing it in an ImageView. 我正在从XML资源读取矢量图像,并将其显示在ImageView中。 It's Ok. 没关系。 Now, I need to pass the image to an Intent using intent.putExtra. 现在,我需要使用intent.putExtra将图像传递给Intent。 Question: How can I convert the vector XML to a bitmap or get the image from the ImageView? 问题:如何将矢量XML转换为位图或从ImageView获取图像? I tried .getDrawingCache(), .getDrawable, getImageMatrix, etc, but it does not work. 我尝试了.getDrawingCache()、. getDrawable,getImageMatrix等,但是它不起作用。

Tried this way: 尝试过这种方式:

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"));

And this way: 这样:

    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, TIA,

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

One of the ways is to use MediaStore : 一种方法是使用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);
}

and than: 然后:

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"));
}

A bit more complicated solution is to use FileProvider Credits: link , link , link 更复杂的解决方案是使用FileProvider信用: linklinklink

I know it was a long time ago, but if someone ends up having this same doubt, I hope it helps. 我知道那是很久以前的事了,但是如果有人最终有同样的疑问,我希望它能有所帮助。 I wanted to share an image and a text, as I couldn't do it the way I'd like, I made a screenshot and shared it. 我想共享图像和文本,因为我无法按照自己的方式进行操作,因此制作了屏幕截图并进行了共享。

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