繁体   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