问题描述
我正在从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)
1楼
一种方法是使用 :
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"));
}
 
  更复杂的解决方案是使用信用: , ,
2楼
我知道那是很久以前的事了,但是如果有人最终有同样的疑问,我希望它能有所帮助。 我想共享图像和文本,因为我无法按照自己的方式进行操作,因此制作了屏幕截图并进行了共享。
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();
}
  
   
  }