0

我正在从 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,

安德烈·科雷亚

4

2 回答 2

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 Credits: link , link , link

于 2017-02-13T22:54:32.403 回答
0

我知道那是很久以前的事了,但如果有人最终有同样的疑问,我希望它会有所帮助。我想分享一张图片和一段文字,因为我不能按照我想要的方式做,所以我做了一个截图并分享了它。

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

}

于 2018-03-21T23:04:23.573 回答